diff --git a/pom.xml b/pom.xml index 0465ad63..a50e2070 100644 --- a/pom.xml +++ b/pom.xml @@ -244,7 +244,6 @@ org.apache.maven.plugins maven-invoker-plugin - ${project.build.directory}/local-repo 1 + + + ${project.build.directory}/local-repo + ${project.build.directory}/local-repo src/it/projects diff --git a/src/it/projects/attach_skip/invoker.properties b/src/it/projects/attach_skip/invoker.properties new file mode 100644 index 00000000..cecfb8c7 --- /dev/null +++ b/src/it/projects/attach_skip/invoker.properties @@ -0,0 +1,17 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +invoker.goals = clean install diff --git a/src/it/projects/attach_skip/pom.xml b/src/it/projects/attach_skip/pom.xml new file mode 100644 index 00000000..e8f36de5 --- /dev/null +++ b/src/it/projects/attach_skip/pom.xml @@ -0,0 +1,109 @@ + + + + + 4.0.0 + org.apache.maven.plugins + maven-jlink-plugin-it-attach-skip + 1.0 + jar + IT-attach-skip + https://maven.apache.org + Test jlink plugin skips attaching output artifacts when attach parameter set to false. + + 9 + UTF-8 + + + + + localhost + first-jar-module-info + 99.0 + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + + + org.apache.maven.plugins + maven-jlink-plugin + @project.version@ + true + + + + + create-runtime-image-1 + package + + jlink + + + + jlink1 + + false + + + + + + create-runtime-image-2 + package + + jlink + + + + + false + + + + + + create-runtime-image-3 + package + + jlink + + + + jlink3 + + true + + + + + + + + diff --git a/src/it/projects/attach_skip/verify.groovy b/src/it/projects/attach_skip/verify.groovy new file mode 100644 index 00000000..d67a5b67 --- /dev/null +++ b/src/it/projects/attach_skip/verify.groovy @@ -0,0 +1,92 @@ + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.*; +import java.util.*; +import java.util.jar.*; +import org.codehaus.plexus.util.*; + +try { + File target = new File( basedir, "target" ) + if ( !target.exists() || !target.isDirectory() ) { + System.err.println( "target file is missing or not a directory." ) + return false + } + + // Check script variable was passed correctly from parent POM + if (!binding.hasVariable('localRepoStr') || localRepoStr == null || localRepoStr.isEmpty()) { + System.err.println("localRepoStr script variable not set or null.") + return false + } + + // Get local repository path from script variable and validate it exists + File localRepo = new File(localRepoStr) + System.out.println("[INFO] localRepo set to: " + localRepo.getAbsolutePath()) + + if ( !localRepo.exists() || !localRepo.isDirectory() ) { + System.err.println("localRepo not set to a valid directory.") + return false + } + + + // Check that output artifacts have been created + File artifact1 = new File( target, "maven-jlink-plugin-it-attach-skip-1.0-jlink1.zip" ) + if ( !artifact1.exists() || artifact1.isDirectory() ) { + System.err.println( "maven-jlink-plugin-it-attach-skip-1.0-jlink1.zip file is missing or is a directory." ) + return false + } + + File artifact2 = new File( target, "maven-jlink-plugin-it-attach-skip-1.0.zip" ) + if ( !artifact2.exists() || artifact2.isDirectory() ) { + System.err.println( "maven-jlink-plugin-it-attach-skip-1.0.zip file is missing or is a directory." ) + return false + } + + File artifact3 = new File( target, "maven-jlink-plugin-it-attach-skip-1.0-jlink3.zip" ) + if ( !artifact3.exists() || artifact3.isDirectory() ) { + System.err.println( "maven-jlink-plugin-it-attach-skip-1.0-jlink3.zip file is missing or is a directory." ) + return false + } + + + // Check that the artifacts' installation status is as expected based on the attach parameter + installedArtifact1 = new File( localRepo, "org/apache/maven/plugins/maven-jlink-plugin-it-attach-skip/1.0/maven-jlink-plugin-it-attach-skip-1.0-jlink1.zip" ) + if ( installedArtifact1.exists() ) { + System.err.println( "maven-jlink-plugin-it-attach-skip-1.0-jlink1.zip WAS installed when attach parameter set to FALSE." ) + return false + } + + installedArtifact2 = new File( localRepo, "org/apache/maven/plugins/maven-jlink-plugin-it-attach-skip/1.0/maven-jlink-plugin-it-attach-skip-1.0.zip" ) + if ( installedArtifact2.exists() ) { + System.err.println( "maven-jlink-plugin-it-attach-skip-1.0.zip WAS installed when attach parameter set to FALSE." ) + return false + } + + installedArtifact3 = new File( localRepo, "org/apache/maven/plugins/maven-jlink-plugin-it-attach-skip/1.0/maven-jlink-plugin-it-attach-skip-1.0-jlink3.zip" ) + if ( !installedArtifact3.exists() ) { + System.err.println( "maven-jlink-plugin-it-attach-skip-1.0-jlink3.zip NOT installed when attach parameter set to TRUE." ) + return false + } + + return true +} catch( Throwable e ) { + e.printStackTrace() + return false +} diff --git a/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java b/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java index fd14f485..368cf863 100644 --- a/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java +++ b/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java @@ -334,6 +334,14 @@ public class JLinkMojo extends AbstractJLinkMojo { @Parameter private File sourceJdkModules; + /** + * Controls whether the plugin tries to attach the resulting artifact to the project. + * + * @since 3.2.1 + */ + @Parameter(defaultValue = "true") + private boolean attach; + /** * Classifier to add to the artifact generated. If given, the artifact will be attached * as a supplemental artifact. @@ -453,15 +461,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { File createZipArchiveFromImage = createZipArchiveFromImage(buildDirectory, outputDirectoryImage); - if (hasClassifier()) { - projectHelper.attachArtifact(getProject(), "jlink", getClassifier(), createZipArchiveFromImage); - } else { - if (projectHasAlreadySetAnArtifact()) { - throw new MojoExecutionException("You have to use a classifier " - + "to attach supplemental artifacts to the project instead of replacing them."); - } - getProject().getArtifact().setFile(createZipArchiveFromImage); - } + attachArtifactUnlessDisabled(createZipArchiveFromImage); } /** @@ -614,6 +614,22 @@ private File createZipArchiveFromImage(File outputDirectory, File outputDirector return resultArchive; } + private void attachArtifactUnlessDisabled(File artifactFile) throws MojoExecutionException { + if (!attach) { + return; + } + + if (hasClassifier()) { + projectHelper.attachArtifact(getProject(), "jlink", getClassifier(), artifactFile); + } else { + if (projectHasAlreadySetAnArtifact()) { + throw new MojoExecutionException("You have to use a classifier " + + "to attach supplemental artifacts to the project instead of replacing them."); + } + getProject().getArtifact().setFile(artifactFile); + } + } + private void failIfParametersAreNotInTheirValidValueRanges() throws MojoFailureException { if (endian != null && (!"big".equals(endian) && !"little".equals(endian))) { String message = "The given endian parameter " + endian