From 26dfd962c5d24da50f14703044c64e1b0ce36622 Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 19 Jul 2025 18:31:49 +1000 Subject: [PATCH 1/4] Add support for optional attach parameter to disable artifact attachment --- .../apache/maven/plugins/jlink/JLinkMojo.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) 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 fd14f48..259754f 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,14 +461,16 @@ 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."); + if (attach) { + 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); } - getProject().getArtifact().setFile(createZipArchiveFromImage); } } From a73468ea5a1c42970b2b39ca5697b2df3fcf814b Mon Sep 17 00:00:00 2001 From: Peter Date: Sun, 20 Jul 2025 18:46:55 +1000 Subject: [PATCH 2/4] Add integration test attach_skip to test jlink plugin correctly skips attaching output artifacts when attach parameter set to false --- pom.xml | 5 +- .../projects/attach_skip/invoker.properties | 17 +++ src/it/projects/attach_skip/pom.xml | 109 ++++++++++++++++++ src/it/projects/attach_skip/verify.groovy | 95 +++++++++++++++ 4 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 src/it/projects/attach_skip/invoker.properties create mode 100644 src/it/projects/attach_skip/pom.xml create mode 100644 src/it/projects/attach_skip/verify.groovy diff --git a/pom.xml b/pom.xml index 0465ad6..a50e207 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 0000000..cecfb8c --- /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 0000000..e8f36de --- /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 0000000..401b199 --- /dev/null +++ b/src/it/projects/attach_skip/verify.groovy @@ -0,0 +1,95 @@ + +/* + * 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 +} From 177d25b09041e4746e23ce6fa40c4cd25282a505 Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 29 Sep 2025 15:56:20 +1000 Subject: [PATCH 3/4] Refactor artifact attachment logic into separate method with guard clauses --- src/it/projects/attach_skip/verify.groovy | 7 ++--- .../apache/maven/plugins/jlink/JLinkMojo.java | 28 +++++++++++-------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/it/projects/attach_skip/verify.groovy b/src/it/projects/attach_skip/verify.groovy index 401b199..d67a5b6 100644 --- a/src/it/projects/attach_skip/verify.groovy +++ b/src/it/projects/attach_skip/verify.groovy @@ -23,8 +23,7 @@ import java.util.*; import java.util.jar.*; import org.codehaus.plexus.util.*; -try -{ +try { File target = new File( basedir, "target" ) if ( !target.exists() || !target.isDirectory() ) { System.err.println( "target file is missing or not a directory." ) @@ -87,9 +86,7 @@ try } return true -} -catch( Throwable e ) -{ +} 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 259754f..ad7d3d8 100644 --- a/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java +++ b/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java @@ -461,17 +461,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { File createZipArchiveFromImage = createZipArchiveFromImage(buildDirectory, outputDirectoryImage); - if (attach) { - 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); } /** @@ -624,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 From e451204801ee8757e6d0434d779479c7fac5cfbc Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 3 Oct 2025 21:56:22 +1000 Subject: [PATCH 4/4] Apply Spotless formatting --- src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ad7d3d8..368cf86 100644 --- a/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java +++ b/src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java @@ -336,7 +336,7 @@ public class JLinkMojo extends AbstractJLinkMojo { /** * Controls whether the plugin tries to attach the resulting artifact to the project. - * + * * @since 3.2.1 */ @Parameter(defaultValue = "true")