maven: With one pom.xml I am creating jar and tar.gz but don't want to deploy jar - maven

With one pom.xml first I am creating a jar file with maven-jar-plugin and signing it with maven-jarsigner-plugin, second I am creating a tar.gz package with maven-assembly-plugin, copying jar file and other necessary files into tar.gz. Just because only tar.gz package is enough for me, I want only tar.gz package to deploy remote repository. When I run the "mvn deploy" command, both the jar and tar.gz packages are being deployed. Are there any method for not to deploy jar file to remote repository.

I tried your suggestion. It is not working for maven-deploy-plugin but it is working for maven-install plugin. Here is the relative part of my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${project.build.directory}/${project.build.finalName}-dev.tar.gz</file>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<packaging>tar.gz</packaging>
<version>${project.version}</version>
<classifier>dev</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<url>http://repo/artifactory/snapshots</url>
<file>${project.build.directory}/${project.build.finalName}-dev.tar.gz</file>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<packaging>tar.gz</packaging>
<version>${project.version}</version>
<classifier>dev</classifier>
</configuration>
</execution>
</executions>
</plugin>
maven-install-plugin is installing only tar.gz file to local repository but maven-deploy-plugin deploying both tar.gz and jar files to remote repository. I think this behaviour can be maven-deploy-plugin's bug.

By default the Maven Deploy plugin will deploy all the artifacts attached to your project or module, i.e. both your .jar and tar.gz file.
deploy:deploy is used to automatically install the artifact, its pom and the attached artifacts produced by a particular project.
What you can do is skip the deploy:deploy goal and configure a personalized deploy:deploy-file goal, such as:
<build>
...
<plugins>
...
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip> <!-- Skip the default deploy -->
</configuration>
<executions>
<!-- Deploy our tar.gz -->
<execution>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<file>${project.build.directory}/${project.build.finalName}.tar.gz</file>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<packaging>tar.gz</packaging>
<version>${project.version}</version>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
You'll have to configure <file> to use your generated tar.gz file.

Related

Spring Boot Executable Jar File Without Dependencies

What is the easiest way to build spring boot jar file without its dependencies?
Basically I should be able to keep dependency jar files in a separate folder.
Currently I'm using spring boot maven plugin, however, it creates a Fat jar file with all dependencies.
Just do not use spring-boot-maven-plugin at all and use JAR packaging. This way the build wouldn't package dependencies into the JAR.
spring-boot-maven-plugin has option for repackaging that puts dependencies inside (making uber jar)
You can disable repackaging or make repackaged .jar go with other classifier [2]
http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html
http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html
Below is a solution I found on How to Create an Executable JAR with Maven,
You just need to put these in your plugins.
<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}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
org.baeldung.executable.ExecutableMavenJar
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Replace change your build entry in pom.xml to
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dependency_jar</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
In the target folder there will be a dependency_jar folder with all the dependency jars, along with "project_name.jar"(fat jar) and "project_name.jar.original"(jar file of your code)

How to upload custom artifact after it was build using a windows bat command?

I have a windows batch file to create me a file myUser.aaa.
And I call this bat file using exec-maven-plugin
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>scripts/MyBat.bat</executable>
</configuration>
</plugin>
</plugins>
What I want to know is how can I install the file to my repo after the MyBat.bat was executed?
I first wanted to use an mvn command from the bat file to upload it but this job gets executed from a Jenkins server and it has its own maven config. If I run mvn from the bat file it will refer to the maven on the local system.
I would suggest to use the build-helper-maven-plugin to add the supplemental artifact to your build and afterwards it will be deployed in one go with the rest which can be done like this:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>some file</file>
<type>extension of your file </type>
<classifier>optional</classifier>
</artifact>
...
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
May be you should bind the exec-maven-plugin to an earlier phase or the build-helper-maven-plugin to a later phase. I would suggest to use prepare-package for the exec-maven-plugin. Furthermore i would suggest to use uptodate versions of the plugins.

Can I produce both jar and war of a project in maven?

I have a project(A) in maven that has packaging of war. One other project(B) depends on A and it needs project A jar file but in phase of compile, the war of project A will produce and no jar is available for project B.
How can I create a jar of project A in phase of compile so that project B can use it?
I would suggest to go a different way and use the maven-war-plugin which can produce a separate artifact for the classes which can be used like the following:
<dependency>
<groupId>myGroup</groupId>
<artifactId>myArtifact</artifactId>
<version>myVersion</myVersion>
<classifier>classes</classifier>
</dependency>
This can be achieved by using the following configuration in your war module:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
...
</plugins>
I found the solution : :)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>
${project.build.directory}/${project.artifactId}-${project.version}.jar
</file>
</configuration>
</execution>
</executions>
</plugin>

maven-deploy-plugin repository

I want to deploy a file using the maven-deploy-plugin. Currently i have the following in my pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>deploy-features-xml</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<repositoryId>${project.distributionManagement.snapshotRepository.id}</repositoryId>
<url>${project.distributionManagement.snapshotRepository.url}</url>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<file>features.xml</file>
</configuration>
</execution>
</executions>
</plugin>
I want to change between the snapshot and release repository based on the version. If the project version is 1-SNAPSHOT the file should be deployed to the snapshot repository, if the project is version 1.0 the file should be deployed to the release repository. But the maven-deploy-plugin hard codes it?
This behaviour is already given by default. But you should use a repository manager. You can simple deploy an artifact via mvn deploy usually having a SNAPSHOT release will go into the SNAPSHOT repository in case of a release it will go to the release repository.
The solution that I ended up with was to use the build-helper-maven-plugin and the maven-resources-plugin. This setup means that along with the jar and pom and project will deploy an xml file that can be references in the maven repo as project/xml/features.
Relevant pom plugins:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>target/features/features.xml</file>
<type>xml</type>
<classifier>features</classifier>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-features</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/features</outputDirectory>
<resources>
<resource>
<directory>src/main/features</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

remove jar created by default in maven

I am using maven assembly plugin. in my pom.xml, pakaging type: jar and i dont use maven jar plugin.
Whenever i run mvn clean package, it create 2 jar files: one is from maven assembly, another one is created by default (due to packaging type =jar). I want to keep only the jar file created by assembly plugin only. How to do that?
You may have your reasons but I doubt that it is a good solution to skip the default jar being built and deployed.
Anyhow here is how you can disable the default jar being built.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<id>make-assembly</id>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- some configuration of yours... -->
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<!-- put the default-jar in the none phase to skip it from being created -->
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

Resources