Skip to content

Commit 45c954f

Browse files
Nicolai Parloggunnarmorling
authored andcommitted
Make Moditect copy dependencies (instead of Maven plugin) 🎶🦄
1 parent 8448593 commit 45c954f

4 files changed

Lines changed: 81 additions & 30 deletions

File tree

‎core/src/main/java/org/moditect/commands/CreateRuntimeImage.java‎

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
import java.io.IOException;
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
22-
import java.nio.file.Paths;
2322
import java.util.ArrayList;
2423
import java.util.Collections;
2524
import java.util.List;
2625
import java.util.Set;
2726
import java.util.stream.Collectors;
2827

2928
import org.moditect.internal.command.ProcessExecutor;
29+
import org.moditect.model.JarInclusionPolicy;
3030
import org.moditect.spi.log.Log;
3131

3232
/**
@@ -36,9 +36,12 @@
3636
*/
3737
public class CreateRuntimeImage {
3838

39+
private static final String DEPENDENCIES_DIRECTORY = "jars";
40+
3941
private final Set<Path> modulePath;
4042
private final List<String> modules;
41-
private final boolean includeJars;
43+
private final JarInclusionPolicy jarInclusionPolicy;
44+
private final Set<Path> dependencies;
4245
private final Path projectJar;
4346
private final Path outputDirectory;
4447
private boolean ignoreSigningInformation;
@@ -51,14 +54,15 @@ public class CreateRuntimeImage {
5154
private final List<String> excludeResourcesPatterns;
5255
private final boolean bindServices;
5356

54-
public CreateRuntimeImage(Set<Path> modulePath, List<String> modules, boolean includeJars, Path projectJar,
55-
String launcherName, String launcherModule,
57+
public CreateRuntimeImage(Set<Path> modulePath, List<String> modules, JarInclusionPolicy jarInclusionPolicy,
58+
Set<Path> dependencies, Path projectJar, String launcherName, String launcherModule,
5659
Path outputDirectory, Integer compression, boolean stripDebug,
5760
boolean ignoreSigningInformation, List<String> excludeResourcesPatterns, Log log,
5861
boolean noHeaderFiles, boolean noManPages, boolean bindServices) {
5962
this.modulePath = ( modulePath != null ? modulePath : Collections.emptySet() );
6063
this.modules = getModules( modules );
61-
this.includeJars = includeJars;
64+
this.jarInclusionPolicy = jarInclusionPolicy;
65+
this.dependencies = dependencies;
6266
this.projectJar = projectJar;
6367
this.outputDirectory = outputDirectory;
6468
this.ignoreSigningInformation = ignoreSigningInformation;
@@ -82,16 +86,39 @@ private static List<String> getModules(List<String> modules) {
8286

8387
public void run() throws IOException {
8488
runJlink();
85-
if (includeJars)
86-
copyJars();
8789
log.info("Done creating image");
90+
copyJars();
8891
}
8992

9093
private void copyJars() throws IOException {
91-
log.info("Copying project JAR");
92-
Path jarDirectory = outputDirectory.resolve("jars");
94+
Path jarDirectory = outputDirectory.resolve(DEPENDENCIES_DIRECTORY);
9395
Files.createDirectories(jarDirectory);
94-
Files.copy(projectJar, jarDirectory.resolve(projectJar.getFileName()));
96+
97+
if (jarInclusionPolicy.includeAppJar()) {
98+
copyAppJar(jarDirectory);
99+
}
100+
if (jarInclusionPolicy.includeDependencies()) {
101+
copyDependencyJars(jarDirectory);
102+
}
103+
}
104+
105+
private void copyAppJar(Path jarDirectory) throws IOException {
106+
log.info("Copying project JAR");
107+
Path target = jarDirectory.resolve(projectJar.getFileName());
108+
Files.copy(projectJar, target);
109+
log.debug(String.format("Done copying app JAR %s to %s", projectJar, target));
110+
}
111+
112+
private void copyDependencyJars(Path jarDirectory) throws IOException {
113+
log.info("Copying project dependencies");
114+
115+
for (Path dependency : dependencies) {
116+
Path target = jarDirectory.resolve(dependency.getFileName());
117+
Files.copy(dependency, target);
118+
log.debug(String.format("Done copying dependency %s to %s", dependency, target));
119+
}
120+
121+
log.info("Done copying project dependencies");
95122
}
96123

97124
private void runJlink() throws AssertionError {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2017 - 2018 The ModiTect authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.moditect.model;
17+
18+
public enum JarInclusionPolicy {
19+
NONE, APP, APP_WITH_DEPENDENCIES;
20+
21+
public boolean includeAppJar() {
22+
return this == APP || this == APP_WITH_DEPENDENCIES;
23+
}
24+
25+
public boolean includeDependencies() {
26+
return this == APP_WITH_DEPENDENCIES;
27+
}
28+
}

‎integrationtest/undertow/pom.xml‎

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<module>java.xml</module>
101101
<module>jdk.unsupported</module>
102102
</modules>
103-
<includeJars>true</includeJars>
103+
<jarInclusionPolicy>APP_WITH_DEPENDENCIES</jarInclusionPolicy>
104104
<launcher>
105105
<name>helloWorld</name>
106106
<mainClass>com.example.HelloWorldServer</mainClass>
@@ -110,22 +110,6 @@
110110
</execution>
111111
</executions>
112112
</plugin>
113-
<plugin>
114-
<groupId>org.apache.maven.plugins</groupId>
115-
<artifactId>maven-dependency-plugin</artifactId>
116-
<version>3.1.2</version>
117-
<executions>
118-
<execution>
119-
<phase>package</phase>
120-
<goals>
121-
<goal>copy-dependencies</goal>
122-
</goals>
123-
<configuration>
124-
<outputDirectory>${project.build.directory}/jlink-image/jars</outputDirectory>
125-
</configuration>
126-
</execution>
127-
</executions>
128-
</plugin>
129113
</plugins>
130114
</build>
131115

‎maven-plugin/src/main/java/org/moditect/mavenplugin/image/CreateRuntimeImageMojo.java‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,21 @@
3333
import org.apache.maven.plugins.annotations.LifecyclePhase;
3434
import org.apache.maven.plugins.annotations.Mojo;
3535
import org.apache.maven.plugins.annotations.Parameter;
36+
import org.apache.maven.plugins.annotations.ResolutionScope;
3637
import org.apache.maven.project.MavenProject;
3738
import org.apache.maven.toolchain.Toolchain;
3839
import org.apache.maven.toolchain.ToolchainManager;
3940
import org.moditect.commands.CreateRuntimeImage;
4041
import org.moditect.mavenplugin.image.model.Launcher;
4142
import org.moditect.mavenplugin.util.MojoLog;
43+
import org.moditect.model.JarInclusionPolicy;
4244

4345
/**
4446
* @author Gunnar Morling
4547
*/
46-
@Mojo(name = "create-runtime-image", defaultPhase = LifecyclePhase.PACKAGE)
48+
@Mojo(name = "create-runtime-image",
49+
defaultPhase = LifecyclePhase.PACKAGE,
50+
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
4751
public class CreateRuntimeImageMojo extends AbstractMojo {
4852

4953
@Parameter(defaultValue = "${project}", readonly = true)
@@ -68,7 +72,7 @@ public class CreateRuntimeImageMojo extends AbstractMojo {
6872
private List<String> modules;
6973

7074
@Parameter
71-
private boolean includeJars;
75+
private JarInclusionPolicy jarInclusionPolicy;
7276

7377
@Parameter
7478
private Launcher launcher;
@@ -119,7 +123,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
119123
CreateRuntimeImage createRuntimeImage = new CreateRuntimeImage(
120124
effectiveModulePath,
121125
modules,
122-
includeJars,
126+
jarInclusionPolicy,
127+
getDirectAndTransitiveDependencies(),
123128
project.getArtifact().getFile().toPath(),
124129
launcher != null ? launcher.getName() : null,
125130
launcher != null ? launcher.getModule() : null,
@@ -141,6 +146,13 @@ public void execute() throws MojoExecutionException, MojoFailureException {
141146
}
142147
}
143148

149+
private Set<Path> getDirectAndTransitiveDependencies() {
150+
return project.getArtifacts()
151+
.stream()
152+
.map(a -> a.getFile().toPath())
153+
.collect(Collectors.toSet());
154+
}
155+
144156
/**
145157
* Returns the directory with the jmod files to be used for creating the image.
146158
* If {@code baseJdk} has been given, the jmod files from the JDK identified that way

0 commit comments

Comments
 (0)