Maven Assembly Plugin, Uploading the right zip file - maven

I have something like the following where I need to generate 2 zip files and finally zip them up to another zip file:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assembly1</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>assembly1</finalName>
<descriptors>
<descriptor>assembly1.xml</descriptor>
</descriptors>
<attach>true</attach>
</configuration>
</execution>
<execution>
<id>assembly2</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>assembly2</finalName>
<descriptors>
<descriptor>assembly2.xml</descriptor>
</descriptors>
<attach>true</attach>
</configuration>
</execution>
<execution>
<id>assembly3</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>assembly3</finalName>
<descriptors>
<descriptor>assembly3.xml</descriptor>
</descriptors>
<attach>true</attach>
</configuration>
</execution>
</executions>
</plugin>
What I am trying to do is creating 2 zip files called assembly1.zip and assembly2.zip, finally zip these 2 zip files into assembly3.zip
Under the target folder, I see the zip files created and in assembly3.zip, I see the assembly1 and 2 zip files as well.
When Maven uploads to the local repo, it randomly picks up one of the zip files and what I want to be uploaded is the assembly3.zip file.
This is what I see in the Maven log:
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (assembly1) # MyTestWar ---
[INFO] Reading assembly descriptor: assembly1.xml
[INFO] Building zip: C:\MyTest\target\assembly1.zip
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (assembly2) # MyTestWar ---
[INFO] Reading assembly descriptor: assembly2.xml
[INFO] Building zip: C:\MyTest\target\assembly2.zip
**[WARNING] Artifact com.test:MyTestWar:zip:1.0.0.11-SNAPSHOT already attached to project, ignoring duplicate**
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (assembly3) # MyTestWar ---
[INFO] Reading assembly descriptor: assembly3.xml
[INFO] Building zip: C:\MyTest\target\assembly3.zip
**[WARNING] Artifact com.test:MyTestWar:zip:1.0.0.11-SNAPSHOT already attached to project, ignoring duplicate**
**[INFO] Installing C:\MyTest\target\assembly1.zip to C:\Users\myUserName\.m2\repository\
com\test\MyTestWar\1.0.0.11-SNAPSHOT\MyTestWar-1.0.0.11-SNAPSHOT.zip**
This is my Maven install plugin where I try to upload Assembly3.zip
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<id>zip-upload</id>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>zip</packaging>
<artifactId>Assembly3</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>${project.build.directory}/assembly3.zip</file>
</configuration>
</execution>
</executions>
</plugin>
Though it is mentioned that it got to upload assembly3.zip, for some reason Maven is picking up assembly1.zip.
Also I am interested in those Warnings.
Any help in getting rid of the warnings as well as uploading Assembly3.zip is highly appreciated.
Thanks

Use
<attach>true</attach>
only for the assembly you want to upload. For the other two executions, omit this option.
As for why it is picking a random assembly instead of uploading all three, I'm guessing the assembly ID in your assembly descriptors are all the same. The error message
[WARNING] Artifact com.test:MyTestWar:zip:1.0.0.11-SNAPSHOT already attached to project, ignoring duplicate
hints at this. Even if you have given the zips different finalName values, they still inherit the classifier from the ID in the assembly descriptor.

Related

How to skip the build of jar file "app-source.jar" when maven build happens in SpringBoot?

I have following jar's when in run mvn clean package command
app.jar
app-source.jar
app.jar.original
I dont want the app-source.jar to be generated when I run the above maven command.
I have added
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<configuration>
<finalName>app</finalName>
<addResources>false</addResources>
<arguments>-Dmaven.source.skip=true</arguments>
<includeSystemScope>false</includeSystemScope>
</configuration>
<executions>
<!--<execution>
<id>skip-sources</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
The below specified attribute 'attach' will disable the sources.jar not included in the artifact list
<attach>false</attach>
</configuration>
</execution>-->
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<!--The below specified attribute 'attach' will disable the sources.jar not included in the artifact list-->
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
SprinBoot Version : 2.7.5
Maven Source Plugin version : 3.2.0
The goal executed in maven build
[INFO] --- maven-source-plugin:3.2.0:jar-no-fork (attach-sources) # app
[INFO] Building jar: path\to\project\target\app-sources.jar
Thanks
Rohit

Creating an Archetype From POM: Property is Missing

I created a Maven archetype and want to create an example project of it in my repository, which seems to be an unusual use-case.
Since I don't want to create the archetype manually, I added the following execution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<configuration>
<archetypeGroupId>my.company.archetypes</archetypeGroupId>
<archetypeVersion>${project.version}</archetypeVersion>
<groupId>org.acme</groupId>
<version>0.1.2-SNAPSHOT</version>
<interactiveMode>false</interactiveMode>
</configuration>
<executions>
<execution>
<id>archetype-one</id>
<goals>
<goal>generate</goal>
</goals>
<phase>package</phase>
<configuration>
<archetypeArtifactId>archetype-one</archetypeArtifactId>
<artifactId>one</artifactId>
<package>org.acme.one</package>
</configuration>
</execution>
</executions>
</plugin>
This leads to the following exception:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:generate (archetype-one) on project examples: Archetype my.company.archetypes:archetype-one:0.9.0-SNAPSHOT is not configured
[ERROR] Property groupId is missing.
[ERROR] Property artifactId is missing.
[ERROR] Property package is missing.
Which is just not true, since I've defined all of these. At least the IDE proposes these tags on that position. Moving the configuration tags around doesn't help either.
So I checked the source code of generate, and lo and behold, the target GAVs aren't present.
How do I define them when generating an archetype directly from another pom.xml?
So I ended up using an entirely different Maven plug-in:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>archetype-one</id>
<goals>
<goal>exec</goal>
</goals>
<phase>package</phase>
<configuration>
<executable>mvn</executable>>
<arguments>
<argument>archetype:generate</argument>
<argument>-DarchetypeGroupId=my.company.archetypes</argument>
<argument>-DarchetypeVersion=${project.version}</argument>
<argument>-DgroupId=org.acme</argument>
<argument>-Dversion=0.1.2-SNAPSHOT</argument>
<argument>-DinteractiveMode=false</argument>
<argument>-DarchetypeArtifactId=archetype-one</argument>
<argument>-DartifactId=one</argument>
<argument>-Dpackage=org.acme.one</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
And since I wanted to generate multiple archetypes, I put the first six arguments into a general <configuration> block, and appended the remaining three with <arguments combine.children="append">.

maven nsis: Avoid duplicate file uploading to nexus

I am using maven nsis-maven-plugin.1.0 plugin which creates myapp-1.0.1_Setup_INTERNAL.exe under the target folder.
And this internally invokes the maven-install-plugin:2.3.1:install and attaches the myapp-1.0.1.exe.
Here is my confusion, even though I mentioned the <outputFile> tag with my preferred .exe file, the NSIS is not considering it.
So, finally there are 2 .exes which get uploaded to Nexus. One is myapp-1.0.1_Setup_INTERNAL.exe and the other is myapp-1.0.1.exe.
I want to avoid uploading the file myapp-1.0.1.exe.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>nsis-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<makensisBin>${project.build.directory}/NSIS/makensis.exe</makensisBin>
<outputFile>${project.build.directory}/${project.build.finalName}_Setup_INTERNAL.exe</outputFile>
<setupScript>${project.build.directory}/gen/internal-installer.nsi</setupScript>
</configuration>
</plugin>

Change jar filename for output of maven jar-with-dependencies?

I create a JAR containing the code from several projects with the maven-assembly-plugin's jar-with-dependencies descriptorRef as described here: Including dependencies in a jar with Maven.
But how do I get it to produce output file foo-1.0.jar instead of the ugly foo-1.0-SNAPSHOT-jar-with-dependencies.jar? I don't need the default JAR that doesn't contain other projects.
In the maven-assembly-plugin you can add an optional parameter called appendAssemblyId (which is set to true by default) in the configuration tag of your assembly execution.
The use of this tag will generate two warnings indicating that you might override the main build of the artifact (done by the maven-jar-plugin).
If you don't want to override this jar with appendAssemblyId set to false, you can decide to build your assembly in another folder with the property outputDirectory.
Or the other solution if you are ok with the fact to have append something at the end of the name of your jar is to create your own assembly descriptor.
(for more information about the existing parameters or how to create your own assembly descriptor you can see the plugin documentation here : https://maven.apache.org/plugins/maven-assembly-plugin/assembly-mojo.html)
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<outputDirectory>${project.build.directory}/my-assembly/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Edit : I edited my answer in order to make it more complete.
Here's what I ended up doing. In my pom.xml:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<id>all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>all.xml</descriptor>
</descriptors>
</configuration>
</plugin>
I specify a home-grown assembly descriptor, which I get by copying the assembly file jar-with-dependencies.xml from https://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html into local file all.xml, changing id jar-with-dependencies to all. VoilĂ , now the generated filename is foo-1.0-SNAPSHOT-all.jar, which works for my purposes.

How to register a custom built jar file as maven main artifact?

I have a project expected to deliver a jar file:
<packaging>jar</packaging>
but the jar is built in a custom way, so the default packaging done with jar:jar has been disabled
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
but then when I want to apply shade:shade on the existing jar I get an error
The project main artifact does not exist.
I assume that maven doesn't know about the .jar file created by my custom tool. How to let it know, because antrun attachArtifact doesn't work
<attachartifact file="./bin/classes.jar" classifier="" type="jar"/>
the error I get is
An Ant BuildException has occured: org.apache.maven.artifact.InvalidArtifactRTException: For artifact {:jar}: An attached artifact must have a different ID than its corresponding main artifact.
So this is not the method to register main artifact... Is there any (without writing custom java plugin)?
Thanks,
Lukasz
I checked the sources of JarMojo and it gave me an idea how to solve it with Groovy (via gmaven)
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>set-main-artifact</id>
<phase>package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
project.artifact.setFile(new File("./bin/classes.jar"))
</source>
</configuration>
</execution>
</executions>
</plugin>
and it works!:)
Something like this
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${basedir}/bin/classes.jar</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
While your solution may work for a build to the install+ phase or where there are no dependencies in the reactor, in cases where only building to the compile or test phase the unpackaged classes won't be found by dependencies.
Building to compile happens when using plugins like the maven-release-plugin.
Extending your chosen solution to include identifying the unpacked classes during compile
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>set-main-artifact-compile</id>
<phase>compile</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
project.artifact.setFile(new File("./bin/classes"))
</source>
</configuration>
</execution>
<execution>
<id>set-main-artifact</id>
<phase>package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
project.artifact.setFile(new File("./bin/classes.jar"))
</source>
</configuration>
</execution>
</executions>
</plugin>
By default the maven-install-plugin will use the identified artifact along the lines of
${project.build.directory}/${project.finalname}.jar
So another option might go something like this
<build>
<directory>bin</directory>
<outputDirectory>bin/classes</outputDirectory>
<finalName>classes</finalName>
</build>
We were having the same problem, with getting the "attached artifact must have a different ID than its corresponding main artifact" error. We found the solution in the following excellent blog post:
embed-and-run-ant-tasks-and-scripts-from-maven
As detailed in this section, you can fix the problem by adding a classifier so Maven can distinguish between the ant-built jar and the maven-built jar. Since you're using antrun attachartifact, you'd need this:
<attachartifact file="./bin/classes.jar" classifier="foo" type="jar"/>
Note you'll also need to include that classifier (along with groupId, artifactId and version) whenever you want to grab this jar as a dependency in other projects.

Resources