Skip to content

Commit 7ea5c23

Browse files
committed
Merge pull request #22 from chringwer/copy-obj-and-libs
Add custom headers and libraries to working dir
2 parents f793e0c + a072857 commit 7ea5c23

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/main/java/com/akathist/maven/plugins/launch4j/Launch4jMojo.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
import java.io.FileOutputStream;
2323
import java.io.IOException;
2424
import java.io.InputStream;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
28+
import java.nio.file.StandardCopyOption;
29+
import java.util.ArrayList;
2530
import java.util.Date;
2631
import java.util.Enumeration;
2732
import java.util.List;
@@ -281,6 +286,8 @@ public void execute() throws MojoExecutionException {
281286
printState();
282287
}
283288

289+
File workdir = setupBuildEnvironment();
290+
284291
Config c = new Config();
285292

286293
c.setHeaderType(headerType);
@@ -297,8 +304,8 @@ public void execute() throws MojoExecutionException {
297304
c.setRestartOnCrash(restartOnCrash);
298305
c.setManifest(manifest);
299306
c.setIcon(icon);
300-
c.setHeaderObjects(objs);
301-
c.setLibs(libs);
307+
c.setHeaderObjects(relativizeAndCopy(workdir, objs));
308+
c.setLibs(relativizeAndCopy(workdir, libs));
302309
c.setVariables(vars);
303310

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

323330
ConfigPersister.getInstance().setAntConfig(c, getBaseDir());
324-
File workdir = setupBuildEnvironment();
325331
Builder b = new Builder(new MavenLog(getLog()), workdir);
326332

327333
try {
@@ -455,6 +461,36 @@ private void setPermissions(File workdir) {
455461
}
456462
}
457463

464+
/**
465+
* If custom header objects or libraries shall be linked, they need to sit inside the launch4j working dir.
466+
*/
467+
private List<String> relativizeAndCopy(File workdir, List<String> paths) throws MojoExecutionException {
468+
if(paths == null) return null;
469+
470+
List<String> result = new ArrayList<>();
471+
for(String path : paths) {
472+
Path source = basedir.toPath().resolve(path);
473+
Path dest = workdir.toPath().resolve(basedir.toPath().relativize(source));
474+
475+
if(!source.startsWith(basedir.toPath())) {
476+
throw new MojoExecutionException("File must reside in the project directory: " + path);
477+
}
478+
479+
if (Files.exists(source)) {
480+
try {
481+
Path target = Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
482+
result.add(workdir.toPath().relativize(target).toString());
483+
} catch (IOException e) {
484+
throw new MojoExecutionException("Can't copy file to workdir", e);
485+
}
486+
} else {
487+
result.add(path);
488+
}
489+
}
490+
491+
return result;
492+
}
493+
458494
/**
459495
* Downloads the platform-specific parts, if necessary.
460496
*/

0 commit comments

Comments
 (0)