Skip to content

Commit d39d6d8

Browse files
Fixing tests and docs (WIP)
1 parent 1a90901 commit d39d6d8

File tree

16 files changed

+191
-62
lines changed

16 files changed

+191
-62
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ Build Configuration]. It is also possible to customize the plugin within a
118118
`<skipNativeBuild>`::
119119
To skip generation of the native image, supply
120120
`<skipNativeBuild>true</skipNativeBuild>` in the configuration of the plugin.
121+
`<skipNativeTests>`::
122+
To skip generation and execution of the native image compiled tests, supply
123+
`<skipNativeTests>true</skipNativeTests>` to the configuration of the plugin.
124+
`<debug>`::
125+
If you want to enable generation of debugging information supply
126+
`<debug>true</debug>` in the configuration of the plugin.
127+
`<verbose>`::
128+
If you want to enable verbose output during native-image building supply
129+
`<verbose>true</verbose>` in the configuration of the plugin.
130+
121131
`<agent>`::
122132
Configuration of the <<agent-support, native agent>>. See <<agent-support-enabling>>
123133
and <<agent-support-configuring-options>> for details.

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/dsl/NativeImageOptions.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.gradle.api.tasks.Optional;
5757
import org.gradle.jvm.toolchain.JavaLauncher;
5858

59+
import java.util.List;
5960
import java.util.Map;
6061

6162

@@ -180,14 +181,14 @@ public interface NativeImageOptions extends Named {
180181
ConfigurableFileCollection getConfigurationFileDirectories();
181182

182183
/**
183-
* Returns the map that as contains information about configuration that should be excluded
184-
* during image building. It consists of a jar regular expression as a key and a resource
185-
* regular expression as a value.
184+
* Returns the MapProperty that contains information about configuration that should be excluded
185+
* during image building. It consists of a dependency coordinates and a list of
186+
* regular expressions that match resources that should be excluded as a value.
186187
*
187188
* @return a map of filters for configuration exclusion
188189
*/
189190
@Input
190-
MapProperty<String, String> getExcludeConfig();
191+
MapProperty<String, List<String>> getExcludeConfig();
191192

192193
@Nested
193194
NativeResourcesOptions getResources();
@@ -222,7 +223,7 @@ public interface NativeImageOptions extends Named {
222223
/**
223224
* Adds a system property to be used by the native-image builder process.
224225
*
225-
* @param name The name of the property
226+
* @param name The name of the property
226227
* @param value The value for the property. May be null.
227228
* @return this
228229
*/

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/BaseNativeImageOptions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464

6565
import javax.inject.Inject;
6666
import java.util.Arrays;
67+
import java.util.List;
6768
import java.util.Map;
6869
import java.util.stream.Collectors;
6970
import java.util.stream.StreamSupport;
@@ -194,6 +195,16 @@ public String getName() {
194195
@InputFiles
195196
public abstract ConfigurableFileCollection getConfigurationFileDirectories();
196197

198+
/**
199+
* Returns the MapProperty that contains information about configuration that should be excluded
200+
* during image building. It consists of a dependency coordinates and a list of
201+
* regular expressions that match resources that should be excluded as a value.
202+
*
203+
* @return a map of filters for configuration exclusion
204+
*/
205+
@Input
206+
public abstract MapProperty<String, List<String>> getExcludeConfig();
207+
197208
@Nested
198209
public abstract NativeResourcesOptions getResources();
199210

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/NativeImageCommandLineProvider.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343

4444
import org.graalvm.buildtools.gradle.dsl.NativeImageOptions;
4545
import org.graalvm.buildtools.utils.NativeImageUtils;
46+
import org.gradle.api.Project;
4647
import org.gradle.api.Transformer;
48+
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
4749
import org.gradle.api.file.FileSystemLocation;
4850
import org.gradle.api.file.RegularFile;
4951
import org.gradle.api.provider.Provider;
@@ -66,17 +68,20 @@ public class NativeImageCommandLineProvider implements CommandLineArgumentProvid
6668
private final Provider<String> outputDirectory;
6769
private final Provider<RegularFile> classpathJar;
6870
private final Provider<Boolean> useArgFile;
71+
private final Project project;
6972

7073
public NativeImageCommandLineProvider(Provider<NativeImageOptions> options,
7174
Provider<String> executableName,
7275
Provider<String> outputDirectory,
7376
Provider<RegularFile> classpathJar,
74-
Provider<Boolean> useArgFile) {
77+
Provider<Boolean> useArgFile,
78+
Project project) {
7579
this.options = options;
7680
this.executableName = executableName;
7781
this.outputDirectory = outputDirectory;
7882
this.classpathJar = classpathJar;
7983
this.useArgFile = useArgFile;
84+
this.project = project;
8085
}
8186

8287
@Nested
@@ -104,16 +109,32 @@ public List<String> asArguments() {
104109
NativeImageOptions options = getOptions().get();
105110
List<String> cliArgs = new ArrayList<>(20);
106111

107-
options.getExcludeConfig().get().forEach((jarPath, resourcePattern) -> {
108-
cliArgs.add("--exclude-config");
109-
cliArgs.add(jarPath);
110-
cliArgs.add(resourcePattern);
112+
options.getExcludeConfig().get().forEach((dependency, listOfResourcePatterns) -> {
113+
// Resolve jar for this dependency.
114+
project.getConfigurations().getByName("runtimeClasspath").getIncoming().artifactView(view -> {
115+
view.setLenient(true);
116+
view.componentFilter(id -> {
117+
if (id instanceof ModuleComponentIdentifier) {
118+
ModuleComponentIdentifier mid = (ModuleComponentIdentifier) id;
119+
String gav = String.format("%s:%s",
120+
mid.getGroup(),
121+
mid.getModule()
122+
);
123+
return dependency.startsWith(gav);
124+
}
125+
return false;
126+
});
127+
}).getFiles().forEach(jarPath -> listOfResourcePatterns.forEach(resourcePattern -> {
128+
cliArgs.add("--exclude-config");
129+
cliArgs.add(jarPath.toPath().toAbsolutePath().toString());
130+
cliArgs.add(String.format("\"%s\"", resourcePattern));
131+
}));
111132
});
112133

113134
cliArgs.add("-cp");
114135
String classpathString = buildClasspathString(options);
115136
cliArgs.add(classpathString);
116-
appendBooleanOption(cliArgs, options.getDebug(), "-H:GenerateDebugInfo=1");
137+
appendBooleanOption(cliArgs, options.getDebug(), "-g");
117138
appendBooleanOption(cliArgs, options.getFallback().map(NEGATE), "--no-fallback");
118139
appendBooleanOption(cliArgs, options.getVerbose(), "--verbose");
119140
appendBooleanOption(cliArgs, options.getSharedLibrary(), "--shared");

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/tasks/BuildNativeImageTask.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@
5656
import org.gradle.api.provider.Property;
5757
import org.gradle.api.provider.Provider;
5858
import org.gradle.api.provider.ProviderFactory;
59-
import org.gradle.api.tasks.Input;
60-
import org.gradle.api.tasks.InputDirectory;
61-
import org.gradle.api.tasks.InputFile;
62-
import org.gradle.api.tasks.Internal;
63-
import org.gradle.api.tasks.Nested;
64-
import org.gradle.api.tasks.Optional;
65-
import org.gradle.api.tasks.OutputDirectory;
66-
import org.gradle.api.tasks.TaskAction;
59+
import org.gradle.api.tasks.*;
6760
import org.gradle.process.ExecOperations;
6861

6962
import javax.inject.Inject;
@@ -155,7 +148,9 @@ private List<String> buildActualCommandLineArgs() {
155148
// a mapped value before the task was called, when we are actually calling it...
156149
getProviders().provider(() -> getOutputDirectory().getAsFile().get().getAbsolutePath()),
157150
getClasspathJar(),
158-
getUseArgFile()).asArguments();
151+
getUseArgFile(),
152+
getProject()
153+
).asArguments();
159154
}
160155

161156
// This property provides access to the service instance
@@ -165,7 +160,6 @@ private List<String> buildActualCommandLineArgs() {
165160
public abstract Property<Object> getService();
166161

167162
@TaskAction
168-
@SuppressWarnings("ConstantConditions")
169163
public void exec() {
170164
List<String> args = buildActualCommandLineArgs();
171165
NativeImageOptions options = getOptions().get();

native-maven-plugin/reproducers/issue-144/pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@
100100
<configuration>
101101
<skip>false</skip>
102102
<imageName>${imageName}</imageName>
103-
<buildArgs>
104-
<buildArg>--no-fallback</buildArg>
105-
</buildArgs>
103+
<fallback>false</fallback>
106104
</configuration>
107105
</plugin>
108106
<plugin>
@@ -117,6 +115,13 @@
117115
<configuration>
118116
<source>${java.version}</source>
119117
<target>1.8</target>
118+
</configuration>
119+
</plugin>
120+
<plugin>
121+
<groupId>org.apache.maven.plugins</groupId>
122+
<artifactId>maven-jar-plugin</artifactId>
123+
<version>3.2.2</version>
124+
<configuration>
120125
<archive>
121126
<manifest>
122127
<addClasspath>true</addClasspath>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ package org.graalvm.buildtools.maven
4444
import spock.lang.Issue
4545

4646
class JavaApplicationFunctionalTest extends AbstractGraalVMMavenFunctionalTest {
47+
def "proper options are added to the native-image invocation"() {
48+
withSample("java-application")
49+
50+
when:
51+
mvn '-Pnative', '-DskipTests', '-DnativeDryRun', '-DuseArgFile=false',
52+
'-Dclasspath=/testcp', '-Ddebug', '-Dfallback=false', '-Dverbose', '-DsharedLibrary',
53+
'-DquickBuild',
54+
'package'
55+
56+
then:
57+
buildSucceeded
58+
outputContains "native-image -cp /testcp -g --no-fallback --verbose --shared -Ob"
59+
}
60+
4761
def "can build and execute a native image with the Maven plugin"() {
4862
withSample("java-application")
4963

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ class MetadataRepositoryFunctionalTest extends AbstractGraalVMMavenFunctionalTes
100100
outputContains "[graalvm reachability metadata repository for org.graalvm.internal:library-with-reflection:1.5]: Configuration directory is org/graalvm/internal/library-with-reflection/1"
101101
}
102102

103+
void "if excludeConfig is set it is added to the command line invocation"() {
104+
given:
105+
withSample("native-config-integration")
106+
107+
when:
108+
mvn '-Pnative,metadataLocal,excludeConfigTest', '-DnativeDryRun', 'package'
109+
110+
then:
111+
buildSucceeded
112+
outputContains "native-image --exclude-config dummy/path/to/file.jar *"
113+
}
114+
103115
void "if the path doesn't exist it throws an error"() {
104116
given:
105117
withSample("native-config-integration")

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ public abstract class AbstractNativeMojo extends AbstractMojo {
142142
@Parameter(property = "sharedLibrary", defaultValue = "false")
143143
protected boolean sharedLibrary;
144144

145+
@Parameter(property = "quickBuild", defaultValue = "false")
146+
protected boolean quickBuild;
147+
145148
@Parameter(property = "useArgFile")
146149
protected Boolean useArgFile;
147150

@@ -169,9 +172,16 @@ public abstract class AbstractNativeMojo extends AbstractMojo {
169172
@Parameter(property = "jvmArgs")
170173
protected List<String> jvmArgs;
171174

175+
@Parameter(property = "agent")
176+
protected Map<String, Object> agent;
177+
// As we are fetching this value by XML parsing in NativeExtension we only need this to keep IDE happy.
178+
172179
@Parameter(alias = "metadataRepository")
173180
protected MetadataRepositoryConfiguration metadataRepositoryConfiguration;
174181

182+
@Parameter(property = NATIVE_IMAGE_DRY_RUN, defaultValue = "false")
183+
protected boolean dryRun;
184+
175185
protected JvmReachabilityMetadataRepository metadataRepository;
176186

177187
@Component
@@ -194,15 +204,15 @@ protected List<String> getBuildArgs() throws MojoExecutionException {
194204
excludeConfig.forEach(entry -> {
195205
cliArgs.add("--exclude-config");
196206
cliArgs.add(entry.getJarPath());
197-
cliArgs.add(entry.getResourcePattern());
207+
cliArgs.add(String.format("\"%s\"", entry.getResourcePattern()));
198208
});
199209
}
200210

201211
cliArgs.add("-cp");
202212
cliArgs.add(getClasspath());
203213

204214
if (debug) {
205-
cliArgs.add("-H:GenerateDebugInfo=1");
215+
cliArgs.add("-g");
206216
}
207217
if (!fallback) {
208218
cliArgs.add("--no-fallback");
@@ -213,6 +223,9 @@ protected List<String> getBuildArgs() throws MojoExecutionException {
213223
if (sharedLibrary) {
214224
cliArgs.add("--shared");
215225
}
226+
if (quickBuild) {
227+
cliArgs.add("-Ob");
228+
}
216229

217230
cliArgs.add("-H:Path=" + outputDirectory.toPath().toAbsolutePath());
218231
cliArgs.add("-H:Name=" + imageName);
@@ -228,7 +241,7 @@ protected List<String> getBuildArgs() throws MojoExecutionException {
228241
}
229242

230243
maybeAddGeneratedResourcesConfig(buildArgs);
231-
maybeAddReachabilityMetadata(cliArgs);
244+
maybeAddReachabilityMetadata(configFiles);
232245

233246
if (configFiles != null && !configFiles.isEmpty()) {
234247
cliArgs.add("-H:ConfigurationFileDirectories=" +
@@ -356,12 +369,10 @@ protected void populateClasspath() throws MojoExecutionException {
356369

357370
protected String getClasspath() throws MojoExecutionException {
358371
populateClasspath();
359-
360372
if (imageClasspath.isEmpty()) {
361373
throw new MojoExecutionException("Image classpath is empty. " +
362374
"Check if your classpath configuration is correct.");
363375
}
364-
365376
return imageClasspath.stream()
366377
.map(Path::toString)
367378
.collect(Collectors.joining(File.pathSeparator));
@@ -387,7 +398,7 @@ protected void buildImage() throws MojoExecutionException {
387398
String commandString = String.join(" ", processBuilder.command());
388399
logger.info("Executing: " + commandString);
389400

390-
if (System.getProperty(NATIVE_IMAGE_DRY_RUN) != null) {
401+
if (dryRun) {
391402
logger.warn("Skipped native-image building due to `" + NATIVE_IMAGE_DRY_RUN + "` being specified.");
392403
return;
393404
}
@@ -460,16 +471,15 @@ public boolean isArtifactExcludedFromMetadataRepository(Artifact dependency) {
460471
}
461472
}
462473

463-
protected void maybeAddReachabilityMetadata(List<String> args) {
474+
protected void maybeAddReachabilityMetadata(List<String> configDirs) {
464475
if (isMetadataRepositoryEnabled() && !metadataRepositoryPaths.isEmpty()) {
465476
String arg = metadataRepositoryPaths.stream()
466477
.map(Path::toAbsolutePath)
467478
.map(Path::toFile)
468479
.map(File::getAbsolutePath)
469480
.collect(Collectors.joining(","));
470-
471481
if (!arg.isEmpty()) {
472-
args.add("-H:ConfigurationFileDirectories=" + arg);
482+
configDirs.add(arg);
473483
}
474484
}
475485
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@
140140
<configuration>
141141
<!-- end::native-plugin-agent-options[] -->
142142
<imageName>${imageName}</imageName>
143-
<buildArgs>
144-
<buildArg>--no-fallback</buildArg>
145-
</buildArgs>
143+
<fallback>false</fallback>
146144
<!-- tag::native-plugin-agent-options[] -->
147145
<agent>
148146
<!-- end::native-plugin-agent-options[] -->
@@ -202,6 +200,14 @@
202200
<configuration>
203201
<source>${java.version}</source>
204202
<target>1.8</target>
203+
</configuration>
204+
</plugin>
205+
206+
<plugin>
207+
<groupId>org.apache.maven.plugins</groupId>
208+
<artifactId>maven-jar-plugin</artifactId>
209+
<version>3.2.2</version>
210+
<configuration>
205211
<archive>
206212
<manifest>
207213
<addClasspath>true</addClasspath>

0 commit comments

Comments
 (0)