Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,17 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<configuration>
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
<environmentVariables>
<!--
! Prevent reading the .mavenrc or maven_pre.bat on Windows
! in integration tests.
-->
<MAVEN_SKIP_RC>1</MAVEN_SKIP_RC>
</environmentVariables>
<!-- Pass local repo directory as script variable to verify.groovy -->
<scriptVariables>
<localRepoStr>${project.build.directory}/local-repo</localRepoStr>
</scriptVariables>
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
<projectsDirectory>src/it/projects</projectsDirectory>
<pomIncludes>
Expand Down
17 changes: 17 additions & 0 deletions src/it/projects/attach_skip/invoker.properties
Original file line number Diff line number Diff line change
@@ -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
109 changes: 109 additions & 0 deletions src/it/projects/attach_skip/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jlink-plugin-it-attach-skip</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>IT-attach-skip</name>
<url>https://maven.apache.org</url>
<description>Test jlink plugin skips attaching output artifacts when attach parameter set to false.</description>
<properties>
<maven.compiler.release>9</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>first-jar-module-info</artifactId>
<version>99.0</version>
</dependency>
</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jlink-plugin</artifactId>
<version>@project.version@</version>
<extensions>true</extensions>

<executions>
<!-- Generate supplemental artifact (i.e. with classifier defined) and configure to skip attaching -->
<execution>
<id>create-runtime-image-1</id>
<phase>package</phase>
<goals>
<goal>jlink</goal>
</goals>

<configuration>
<classifier>jlink1</classifier>
<!-- Skip attaching as an artifact -->
<attach>false</attach>
</configuration>
</execution>

<!-- Generate main artifact (i.e. omitting classifier definition) and configure to skip attaching -->
<execution>
<id>create-runtime-image-2</id>
<phase>package</phase>
<goals>
<goal>jlink</goal>
</goals>

<configuration>
<!-- Skip attaching as an artifact -->
<attach>false</attach>
</configuration>
</execution>

<!-- Generate supplemental artifact (i.e. with classifier defined) and configure to attach -->
<execution>
<id>create-runtime-image-3</id>
<phase>package</phase>
<goals>
<goal>jlink</goal>
</goals>

<configuration>
<classifier>jlink3</classifier>
<!-- Attach as an artifact -->
<attach>true</attach>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
95 changes: 95 additions & 0 deletions src/it/projects/attach_skip/verify.groovy
Original file line number Diff line number Diff line change
@@ -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() ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can simply use a groovy assert

https://groovy-lang.org/semantics.html#_power_assertion

but it is not a blocker for it

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
}
24 changes: 17 additions & 7 deletions src/main/java/org/apache/maven/plugins/jlink/JLinkMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}

Expand Down