Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom headers and libraries to working dir #22

Merged
merged 1 commit into from
Mar 31, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like unused

import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
Expand Down Expand Up @@ -281,6 +286,8 @@ public void execute() throws MojoExecutionException {
printState();
}

File workdir = setupBuildEnvironment();

Config c = new Config();

c.setHeaderType(headerType);
Expand All @@ -297,8 +304,8 @@ public void execute() throws MojoExecutionException {
c.setRestartOnCrash(restartOnCrash);
c.setManifest(manifest);
c.setIcon(icon);
c.setHeaderObjects(objs);
c.setLibs(libs);
c.setHeaderObjects(relativizeAndCopy(workdir, objs));
c.setLibs(relativizeAndCopy(workdir, libs));
c.setVariables(vars);

if (classPath != null) {
Expand All @@ -321,7 +328,6 @@ public void execute() throws MojoExecutionException {
}

ConfigPersister.getInstance().setAntConfig(c, getBaseDir());
File workdir = setupBuildEnvironment();
Builder b = new Builder(new MavenLog(getLog()), workdir);

try {
Expand Down Expand Up @@ -455,6 +461,36 @@ private void setPermissions(File workdir) {
}
}

/**
* If custom header objects or libraries shall be linked, they need to sit inside the launch4j working dir.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the objs and libs must be relative to project's base dir?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems to be the way how it is implemented in launch4j. It will try to construct an absolute path using new File(_basedir, pathToMyHeader) which results in "C:...m2..\workdir\C:..\projectdir". Now, when I use your plugin I don't wanna make assumptions on where that working dir may reside and try building relative paths.

Let me know if there is a better way to acheive this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I assume you have tested this solution?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I've tested a configuration like this:

<objs>
    <obj>w32api/crt2.o</obj>
    <obj>${basedir}/src/main/native/copy_of_guihead.o</obj>
    <obj>src/main/native/copy_of_head.o</obj>
</objs>

I copied the default header objects from the launch4j sources into my project dir and renamed them. Also, I linked w32api/crt2.0 which is already part of the working directory and not included into my project sources. This will produce a running launcher even if an absolute path is specified by using ${basedir}.

Of course, I made sure nothing will break if header objects aren't used at all.

*/
private List<String> relativizeAndCopy(File workdir, List<String> paths) throws MojoExecutionException {
if(paths == null) return null;

List<String> result = new ArrayList<>();
for(String path : paths) {
Path source = basedir.toPath().resolve(path);
Path dest = workdir.toPath().resolve(basedir.toPath().relativize(source));

if(!source.startsWith(basedir.toPath())) {
throw new MojoExecutionException("File must reside in the project directory: " + path);
}

if (Files.exists(source)) {
try {
Path target = Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
result.add(workdir.toPath().relativize(target).toString());
} catch (IOException e) {
throw new MojoExecutionException("Can't copy file to workdir", e);
}
} else {
result.add(path);
}
}

return result;
}

/**
* Downloads the platform-specific parts, if necessary.
*/
Expand Down