How to copy war file to multiple folders? - java

It's possible to change the target build path where the war file is placed on mvn package by:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<outputDirectory>my\target\folder</outputDirectory>
</configuration>
</plugin>
Question: how can I create the war file in the default /target, folder, but additionally copy the war file to one (or multiple) other destinations after build?

A simple way to do that would be to bind an execution of the maven-antrun-plugin to the package phase. The advantage is that you don't need to re-execute the maven-war-plugin like mentioned in this answer.
This execution would copy the main artifact to the folder /path/to/folder.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${project.build.finalName}.war"
todir="/path/to/folder" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
(This snippet must be placed inside the <build><plugins> element).
Running mvn clean install (or "Run As... > Maven Install" in Eclipse), Maven will do what you want. ${project.build.directory}/${project.build.finalName}.war refers to the main WAR artifact present in the build directory (which is target by default).

Related

why is 'dependency:copy-dependencies' executed unexpectedly during tycho build?

I want to add JARs to a plugin. Previously, I added them by hand but thought it would be a good idea to add them via dependency definitions.
I created a pom.xml in the bundle and added an execution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/lib</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
As soon as I save the file the dependency JARs are all copied to the lib folder. So far so good...
When I start a tycho build, such as mvn install, the build succeeds but the lib folder now contains a new folder lib/myplugin-1.0.0.qualifier-lib and this folder contains copies of all the JARs in lib but they all have the extension .jar.jar.
If I remove the copy-dependencies execution before starting the tycho build then this folder is not created.
Can anyone explain why this is happening and what I should do to avoid this behaviour?

Copy resulting fat jar to another target folder in Spring boot project

I have Spring boot project. Everything is built OK in /target folder but I'd like to change destination folder.
I tried to use maven-jar-plugin as described below but it copies only jar with compiled classes of project (small jar).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<outputDirectory>${maven.multiModuleProjectDirectory}/output/bin</outputDirectory>
</configuration>
</plugin>
Instead I want to make Maven to copy FAT jar.
Maven builds FAT jar and puts the Jar to /target folder. How to change destination?
UPDATE 1
this is needed by project requirements - need to build folder structure with properties, bash scripts(as entry point) and fat jar:
Try to add this plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
<outputDirectory>${maven.multiModuleProjectDirectory}/output/bin</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

How to add executable file in resources folder in Spring Boot

Requirement: To use an executable file within my resources folder
Limitation: I need to have that executable file within the resources folder, that is, I cannot have the executable file be present on the server from before
Scenario: When I am adding the executable file in the resources folder and then I am building the project using mvn clean install, then I have the following target folder structure.
target -> classes -> myExecutableFile
Problem: myExecutableFile present inside the target folder does not have executable permissions. Is there any way I can retain the executable permission for the file?
You could do this via Ant by doing something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>process-resources</id>
<phase>process-resources</phase>
<configuration>
<target>
<chmod file="target/script.sh" perm="755"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Copy file to jar root when building spring-boot project with Maven

I have a Spring Boot project and I'm building the jar file with mvn clean build.
I need to copy a folder and a file to the root of the jar, where META-INF is located. I tried maven-resources-plugin but I can't reach my goal.
For war files I used maven-war-plugin in the past but I can't find something similar for jars.
Can anybody give me an idea?
Thanks.
I could manage to add files after the repackage goal of the spring-boot-maven-plugin using the maven-antrun-plugin and the Jar tool included in the JDK.
Updating a JAR File
The Jar tool provides a u option which you can use to update the contents of an existing JAR file by modifying its manifest or by adding files.
The basic command for adding files has this format:
jar uf jar-file input-file(s)
https://docs.oracle.com/javase/tutorial/deployment/jar/update.html
Using Maven-Antrun-Plugin
This command can be executed using the maven-antrun-plugin together with the exec task, which allows you to execute commands via Runtime.exec(..).
The entry may look like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<exec executable="jar" vmlauncher="false">
<arg value="uf" />
<arg value="${project.build.directory}/myprogram.jar" />
<arg value="-C" />
<arg value="${project.build.directory}/classes" />
<arg value="org/company/app/Main.class" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Notice the -C option, which allows you to change the directory that should not be included in the jar.
https://maven.apache.org/plugins/maven-antrun-plugin/usage.html
https://ant.apache.org/manual/Tasks/exec.html
Using Exec-Maven-Plugin
Alternatively the exec-maven-plugin can be used which does not require Ant to be installed.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals><goal>exec</goal></goals>
<configuration>
<executable>jar</executable>
<arguments>
<argument>uf</argument>
<argument>${project.build.directory}/myprogram.jar</argument>
<argument>-C</argument>
<argument>${project.build.directory}/classes</argument>
<argument>org/company/app/Main.class</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
The final structure may look like this:
myprogram.jar
|-BOOT-INF/..
|-META-INF/..
|-org/springframework/boot/loader/..
|-org/company/app/Main.class
That way you can add any additional files you want after the jar has been packaged.
could something like the following help?
See also maven-jar-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<includes>
<include>**/cdi/*</include>
<include>**/META-INF/*</include>
<include>*.properties</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
I resolved my problem by using the maven-assembly-plugin. I created an assembly zip which contains the application jar and some other resources.
This solution is specific for applications which use AWS Elastic Beanstalk and need to implement the Procfile (http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-platform.html#java-se-procfile).

Maven: jar-with-dependencies include external jar [duplicate]

I want to build a .jar file with dependencies in maven. Unfortunately I have to include some external .jars in my buildpath. When I now try to build this project with maven package I will get an error that those external .jars are not found.
How to adapt my pom file to add those jars?
current:
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
You can include the external jars in your build path as dependency with <scope>system</scope>
.
Check this link
You need use below command to add external jar into .m2 folder
mvn install:install-file -Dfile=[JAR] -DgroupId=[some.group] -DartifactId=[Some Id] -Dversion=1.0.0 -Dpackaging=jar
This will add the given jar in to your .m2 folder. After that go to pom.xm and just add the dependency with given group id, artifact id and version.
A simple solution for this is to add it into local maven repository
One way to do is via mvn install commands as suggested in previous post .
Another easy way is,
In your eclipse ide right click on project select Maven option.
Select Install or deploy an artifact to a maven repository option and click on next.
Click on browse next to the Artifact file checkbox & select your jar file.
Enter the GroupId and ArtifactId and version ensure generate pom & create checksum are checked & packaging is jar
Click on finish, Wallah!!! your job is done the jar is added in your local repository which you can define in setting.xml or m2 directory.
Now just add the simple maven dependency as per the GroupId,ArtifactId & jar version that you have entered as per the import and that's it your external jar will be packaged by maven.

Categories

Resources