Skip to content

Commit d51b4be

Browse files
committed
Demonstrate how to run tests in shaded mode with Maven
Unfortunately, due to different bugs(?) in Maven, we cannot use the shading plugin for tests, and we have to move the test execution to the integration test phase. See https://issues.apache.org/jira/browse/MSHADE-402
1 parent 9100f62 commit d51b4be

File tree

5 files changed

+215
-2
lines changed

5 files changed

+215
-2
lines changed

docs/src/docs/asciidoc/maven-plugin.adoc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ In this case, the arguments that will be passed to the `native-image` executable
127127
--no-fallback --verbose
128128
```
129129

130+
[[testing-support]]
130131
== JUnit Testing support
131132

132133
In order to use the recommended test listener mode, you need to add following dependency:
@@ -163,7 +164,30 @@ Then the native plugin needs to be configured to use this jar instead of the ful
163164
[source,xml,indent=0]
164165
include::../../../../samples/java-application/pom.xml[tag=native-plugin]
165166

166-
Please refer to the https://maven.apache.org/plugins/maven-shade-plugin[Maven Shade plugin documentation] for more details on how to configure shading.
167+
To be able to <<testing-support,execute tests in native mode>>, you will need more setup:
168+
169+
- Create a `src/assembly/test-jar-with-dependencies.xml` file with the following contents:
170+
171+
[source,xml,indent=0]
172+
include::../../../../samples/java-application-with-tests/src/assembly/test-jar-with-dependencies.xml[tag=assembly]
173+
174+
- Add the assembly plugin to your `native` profile:
175+
176+
[source,xml,indent=0]
177+
include::../../../../samples/java-application-with-tests/pom.xml[tag=assembly-plugin]
178+
179+
- Due to a limitation in Maven, you will need to move the tests execution to the "integration-test" phase:
180+
181+
[source,xml,indent=0]
182+
include::../../../../samples/java-application-with-tests/pom.xml[tag=native-plugin]
183+
184+
Finally, you will need to execute tests using the `integration-test` phase instead of `test`:
185+
186+
```bash
187+
./mvn -Pnative integration-test
188+
```
189+
190+
Please refer to the https://maven.apache.org/plugins/maven-shade-plugin[Maven Shade plugin documentation] for more details on how to configure shading and the https://maven.apache.org/plugins/maven-assembly-plugin[Maven Assembly plugin documentation] to tweak what to include in tests.
167191

168192
== Javadocs
169193

native-maven-plugin/src/functionalTest/groovy/org/graalvm/buildtools/maven/JavaApplicationWithTestsFunctionalTest.groovy

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ class JavaApplicationWithTestsFunctionalTest extends AbstractGraalVMMavenFunctio
6464
[ 0 tests aborted ]
6565
[ 6 tests successful ]
6666
[ 0 tests failed ]
67+
""".trim()
68+
}
69+
70+
def "can run tests in a native image with the Maven plugin using shading"() {
71+
withSample("java-application-with-tests")
72+
73+
when:
74+
mvn '-Pshaded', 'integration-test'
75+
76+
then:
77+
buildSucceeded
78+
outputContains "[junit-platform-native] Running in 'test listener' mode"
79+
outputContains """
80+
[ 3 containers found ]
81+
[ 0 containers skipped ]
82+
[ 3 containers started ]
83+
[ 0 containers aborted ]
84+
[ 3 containers successful ]
85+
[ 0 containers failed ]
86+
[ 6 tests found ]
87+
[ 0 tests skipped ]
88+
[ 6 tests started ]
89+
[ 0 tests aborted ]
90+
[ 6 tests successful ]
91+
[ 0 tests failed ]
6792
""".trim()
6893
}
6994
}

native-maven-plugin/src/main/java/org/graalvm/buildtools/maven/NativeTestMojo.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public class NativeTestMojo extends AbstractNativeMojo {
7979
@Parameter(property = "skipTests", defaultValue = "false")
8080
private boolean skipTests;
8181

82+
@Parameter(property = "classpath")
83+
private List<String> classpath;
84+
8285
@Override
8386
public void execute() throws MojoExecutionException, MojoFailureException {
8487
if (skipTests) {
@@ -182,6 +185,9 @@ private void runTests(Path targetFolder) throws MojoExecutionException {
182185
}
183186

184187
private String getClassPath() throws MojoFailureException {
188+
if (classpath != null && !classpath.isEmpty()) {
189+
return String.join(File.pathSeparator, classpath);
190+
}
185191
try {
186192
List<Artifact> pluginDependencies = pluginArtifacts.stream()
187193
.filter(it -> it.getGroupId().startsWith(Utils.MAVEN_GROUP_ID) || it.getGroupId().startsWith("org.junit"))

samples/java-application-with-tests/pom.xml

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,97 @@
109109
</plugins>
110110
</build>
111111
</profile>
112+
<profile>
113+
<id>shaded</id>
114+
<build>
115+
<plugins>
116+
<!-- tag::shade-plugin[] -->
117+
<plugin>
118+
<groupId>org.apache.maven.plugins</groupId>
119+
<artifactId>maven-shade-plugin</artifactId>
120+
<version>3.2.4</version>
121+
<executions>
122+
<execution>
123+
<phase>package</phase>
124+
<goals>
125+
<goal>shade</goal>
126+
</goals>
127+
</execution>
128+
</executions>
129+
<configuration>
130+
<shadedArtifactAttached>true</shadedArtifactAttached>
131+
</configuration>
132+
</plugin>
133+
<!-- end::shade-plugin[] -->
134+
135+
<!-- tag::assembly-plugin[] -->
136+
<plugin>
137+
<groupId>org.apache.maven.plugins</groupId>
138+
<artifactId>maven-assembly-plugin</artifactId>
139+
<version>2.4.1</version>
140+
<configuration>
141+
<descriptors>
142+
<descriptor>src/assembly/test-jar-with-dependencies.xml</descriptor>
143+
</descriptors>
144+
</configuration>
145+
<executions>
146+
<execution>
147+
<id>make-test-jar</id>
148+
<phase>package</phase>
149+
<goals>
150+
<goal>single</goal>
151+
</goals>
152+
</execution>
153+
</executions>
154+
</plugin>
155+
<!-- end::assembly-plugin[] -->
156+
157+
<!-- tag::native-plugin[] -->
158+
<plugin>
159+
<groupId>org.graalvm.buildtools</groupId>
160+
<artifactId>native-maven-plugin</artifactId>
161+
<version>${native.maven.plugin.version}</version>
162+
<executions>
163+
<execution>
164+
<id>test-native</id>
165+
<goals>
166+
<goal>test</goal>
167+
</goals>
168+
<phase>integration-test</phase>
169+
<configuration>
170+
<classpath>
171+
<param>${project.build.directory}/${project.artifactId}-${project.version}-tests.jar</param>
172+
</classpath>
173+
</configuration>
174+
</execution>
175+
<execution>
176+
<id>build-native</id>
177+
<goals>
178+
<goal>build</goal>
179+
</goals>
180+
<phase>package</phase>
181+
<configuration>
182+
<classpath>
183+
<param>${project.build.directory}/${project.artifactId}-${project.version}-shaded.jar</param>
184+
</classpath>
185+
</configuration>
186+
</execution>
187+
</executions>
188+
<configuration>
189+
<skip>false</skip>
190+
<imageName>${imageName}</imageName>
191+
<buildArgs>
192+
<buildArg>--no-fallback</buildArg>
193+
</buildArgs>
194+
</configuration>
195+
</plugin>
196+
<!-- end::native-plugin[] -->
197+
</plugins>
198+
</build>
199+
</profile>
112200
</profiles>
113201

114202
<build>
115-
<finalName>${project.artifactId}</finalName>
116203
<plugins>
117204
<plugin>
118205
<groupId>org.apache.maven.plugins</groupId>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
4+
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
6+
The Universal Permissive License (UPL), Version 1.0
7+
8+
Subject to the condition set forth below, permission is hereby granted to any
9+
person obtaining a copy of this software, associated documentation and/or
10+
data (collectively the "Software"), free of charge and under any and all
11+
copyright rights in the Software, and any and all patent rights owned or
12+
freely licensable by each licensor hereunder covering either (i) the
13+
unmodified Software as contributed to or provided by such licensor, or (ii)
14+
the Larger Works (as defined below), to deal in both
15+
16+
(a) the Software, and
17+
18+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
19+
one is included with the Software each a "Larger Work" to which the Software
20+
is contributed by such licensors),
21+
22+
without restriction, including without limitation the rights to copy, create
23+
derivative works of, display, perform, and distribute the Software and make,
24+
use, sell, offer for sale, import, export, have made, and have sold the
25+
Software and the Larger Work(s), and to sublicense the foregoing rights on
26+
either these or other terms.
27+
28+
This license is subject to the following condition:
29+
30+
The above copyright notice and either this complete permission notice or at a
31+
minimum a reference to the UPL must be included in all copies or substantial
32+
portions of the Software.
33+
34+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40+
SOFTWARE.
41+
-->
42+
43+
<!-- tag::assembly[] -->
44+
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
45+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
46+
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
47+
<id>tests</id>
48+
<formats>
49+
<format>jar</format>
50+
</formats>
51+
<fileSets>
52+
<fileSet>
53+
<directory>${project.build.directory}/test-classes</directory>
54+
<outputDirectory>/</outputDirectory>
55+
</fileSet>
56+
<fileSet>
57+
<directory>${project.build.outputDirectory}</directory>
58+
<outputDirectory>/</outputDirectory>
59+
</fileSet>
60+
</fileSets>
61+
<includeBaseDirectory>false</includeBaseDirectory>
62+
<dependencySets>
63+
<dependencySet>
64+
<outputDirectory>/</outputDirectory>
65+
<useProjectArtifact>true</useProjectArtifact>
66+
<unpack>true</unpack>
67+
<scope>test</scope>
68+
</dependencySet>
69+
</dependencySets>
70+
</assembly>
71+
<!-- end::assembly[] -->

0 commit comments

Comments
 (0)