Zip/tar the generated jar file along with other file - java

I need to zip the generated jar file after running the mvn clean deploy.
But i am facing problem with it.
I am using assembly plugin and my assembly.xml
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<baseDirectory>/</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/resources/process.sh</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
and pom.xml looks like below.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/assembly/assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- this is used for inheritance merges -->
<phase>package</phase>
<!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
In above case i tried to tar the entire target folder. It got compressed but there was no files.
What am doing is first executing mvn clean then executing mvn clean deploy.
I just want the generated jar/war file to be compressed after execution of mvn clean deploy.
after running mvn clean deploy its zipping the .jar file, its zipping the .jar.original. which is of 8kb file.
Any help will be appreciated.

Related

How do I set file permissions on a file when deploying an AWS Serverless Application with Maven

I want to deploy an AWS lambda function using the AWS Serverless Application Model with Maven. In the lambdas deployment zip file I want to include two external files (file1 and file2) that need to have executable permisions. (chmod 755 / -rwxr-xr-x). The files are both 64-bit ELFs
The files on my local machine have those permisions, however when built and deployed to AWS I can export and download the function from the online AWS lambda console to a ZIP and see that the deployed files now have the permisions -rw-r--r-- (chmod 644).
I have fixed this issue in Gradle before by quite simply doing something like filesMatching('file1') { mode = 0755 }
I am using:
java11
maven-shade-plugin 3.2.2
How do I achieve this in Maven? Here is the build portion of my pom.xml
<build>
<resources>
<resource>
<directory>files</directory>
<includes>
<include>file1</include>
<include>file2</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
For anyone else having this issue, I ended up using maven-assembly-plugin as suggested by khmarbaise. My solution ended up looking like this:
This was the build section of my pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>distribution.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then I added the file "distribution.xml" to the same directory as the POM.xml. Which looked like this.
<assembly
xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>aws-lambda-package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>files</directory>
<outputDirectory>./</outputDirectory>
<includes>
<include>file1</include>
<include>file2</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
As a side effect of doing this I could no longer use the name of the function as the value for the codeURI in the template.yaml. So I needed to change it to the location of the zip file created when maven clean install is ran. Which in most peoples cases will be located: <functionName>/target/<fileName>.zip.

Failed to create assembly: Error creating assembly archive null: A tar file cannot include itself

I'm trying to use maven-assembly-plugin to package my project into a .tar file.
I've added the following plugin to my pom.xml file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptor>src/assembly/myProjectConf.xml</descriptor>
<finalName>my-project-${version}</finalName>
<excludes>
<exclude>**/*.zip</exclude>
<exclude>**/*.tar</exclude>
</excludes>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
myProjectConf.xml has the following content:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/3.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/3.0.0 http://maven.apache.org/xsd/assembly-3.0.0.xsd">
<formats>
<format>tar</format>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main</directory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
<outputDirectory>${project.basedir}/target/packagedFiles</outputDirectory>
</fileSet>
<fileSet>
<directory>src/test</directory>
<outputDirectory>${project.basedir}/target/packagedFiles</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.basedir}/lib</directory>
<outputDirectory>${project.basedir}/target/packagedFiles</outputDirectory>
</fileSet>
</fileSets>
</assembly>
However, I'm getting;
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single (default) on project job-manager: Failed to create assembly: Error creating assembly archive null: A tar file cannot include itself. -> [Help 1]
I looked at this question How to prevent 'Error creating assembly archive distribution: A zip file cannot include itself' that's similar and I therefore added <excludes> to my project, as you can see above.
Nothing changed with the error message though.
I have resolved this issue by adding "maven-clean-plugin".
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>

Create zip in maven with additional files next to the jar

The only Maven experience I have is including other libraries so I need a very basic explanation about how I can achieve things in Maven with Eclipse.
I want to create my jar regulary. Then I want to take 3 further files and put all files together in 1 zip file. The content of my zip should look like this:
A.jar
B.txt
C.txt
D.bat
I know I have to insert certain goals in my pom.xml file. I already saw posts on Stack Overflow (i.e. here) regarding similar topics. I also tried the Maven documentation like here. Do I already need any Maven plugins or is still already Maven-core stuff?
But my skills are not enough yet to transfer this information for my case.
+ project
+ src/main/java
+ src/main/resources
+ src/main/config
+B.txt
+C.txt
+D.bat
+ src/main/assembly
+bin.xml
+pom.xml
bin.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<baseDirectory>/</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/config</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
poml.xml
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/bin.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Output :
mvn clean package
+ artifactId-version.zip
+ B.txt
+C.txt
+D.txt
+artifactId-version.jar
Not added B.txt, C.txt, D.bat in src/main/resources because it's not good to have them in the CLASSSPATH
You need to use the maven-assembly-plugin when you need to create custom artifacts. I strongly suggest that you read Chapter 8. Maven Assemblies of the Maven book to get you started with using assemblies. This chapter contains in-depth explanations on the creation of Maven assemblies, and it is easy to read.
Basically, an assembly is created with the help of an assembly descriptor. The following assembly descriptor will include the project main artifact at the root of a zip archive. You can add more <file> or <fileSets> declaration here to add your custom files in your archive (the logic is the same)
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>assemby-id</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>${project.build.directory}/${project.build.finalName}.${project.packaging}</source>
<outputDirectory>/</outputDirectory>
</file>
</files>
</assembly>
Then you just need to declare this plugin in your POM. In this case, the plugin is bound to the package phase and configured with the descriptor above. By default, the assembly id is appended to the name of the archive: I removed it here.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor> <!-- path to the descriptor -->
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
When running mvn clean package, you will see that a zip archive will have been created in the target directory.

bundling multiple artifact for deployment?

This is follow up based on this answer.
I have a structure that looks like
$ ls service/target/
classes lib maven-status surefire-reports
classes.-1194128992.timestamp maven-archiver service-1.0-SNAPSHOT.jar
and with-in that lib looks like
$ ls service/target/lib/
activation-1.1.jar akka-http-spray-json-experimental_2.11-1.0.jar mail-1.4.7.jar scala-reflect-2.11.2.jar
akka-actor_2.11-2.3.12.jar akka-parsing-experimental_2.11-1.0.jar manager-1.0-SNAPSHOT.jar scala-xml_2.11-1.0.2.jar
akka-http-core-experimental_2.11-1.0.jar akka-stream-experimental_2.11-1.0.jar reactive-streams-1.0.0.jar scalatest_2.11-2.2.5.jar
akka-http-experimental_2.11-1.0.jar config-1.2.1.jar
As part of mvn clean install, I want to bundle my-deployment-artifact which should look contain
service-1.0-SNAPSHOT.jar
lib/* (all the jars here)
How do I create this as a tar or .tar.gz and produce with mvn clean install?
You can use maven-assembly-plugin to do that task.
Create an assembly definition file in src/assembly/distribution.xml
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>distribution</id>
<formats>
<format>tar</format>
</formats>
<files>
<file>
<source>${project.build.directory}/${project.build.finalName}.${project.packaging}</source>
</file>
</files>
<fileSets>
<fileSet>
<directory>${project.build.directory}/lib</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>
</fileSets>
</assembly>
In pom.xml file, add plugin declare, execution phase and goal for it.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptor>${project.basedir}/src/assembly/distribution.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
More format file or customize of maven-assembly-plugin can be found here: https://maven.apache.org/plugins/maven-assembly-plugin/

How to create a release package with maven?

I build a desktop application using Maven2.
I'd like to make a release from time to time (just copy all the project's and third party jars into a single dir and generate a run.bat file).
How to do it ?
You need to create run.bat yourself and place it in src/main/assembly/scripts, for example. Then you need to create an assembly.xml file in src/main/assembly.
Here is an example of an assembly.xml file that you might want to use. It creates a tar.gz with all your dependency jars and your run.bat.
<assembly>
<id>1.0</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/assembly/scripts</directory>
<outputDirectory>/scripts</outputDirectory>
<includes>
<include>*.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Finally, in your pom.xml file add the assembly plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
Now, when you run "mvn install" you should see your tar.gz created.
To release run:
mvn release:prepare
mvn release:perform
OK I got it with a little help of dogbane's answer
I used my own assemlby file src/main/assemlby.assembly.xml
<assembly>
<id>teleinf</id>
<formats>
<format>dir</format>
</formats>
<moduleSets>
<moduleSet>
<includes>
<include>pl..........:core</include>
<include>pl..........:gui</include>
</includes>
<binaries>
<outputDirectory>../../release</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
<fileSets>
<fileSet>
<directory>src/main/assembly/</directory>
<outputDirectory>../../release</outputDirectory>
<includes>
<include>*.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>
and added following to pom
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
I had to write a run.bat myself - so it's not fully satisfying but will do.
i would go with the maven release plugin, see:
maven release plugin
using maven release plugin
Just an addition to the answer given by dogbane
Your .bat file will running packaged jar file with a filename reflecting the current version of the application, e.g.
java -Xms256m -Xmx350m -jar bin\yourApp-1.10.1-SNAPSHOT.jar
On every release this file will need to get updated with a new version name of the application. This can be automatized, too.
In your pom.xml file add this section:
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.build.sourceDirectory}/../assembly/scripts</directory>
<includes>
<include>yourApp.bat</include>
</includes>
</resource>
...
</resources>
...
</build>
This asumes that you have put the yourApp.bat file in the folder:
src/main/assembly/scripts
Content of the yourApp.bat file should look like this:
java -Xms256m -Xmx350m -jar bin\${project.build.finalName}.jar
Just run the Maven commands and enjoy.

Categories