How do I deploy files to Artifactory in Maven? - java

My current pom is configured to deploy a jar to my artifactory.
In addition, there is a bash script that runs the jar and I keep it in my project as well.
I would like to deploy this script as well (separately from the jar) to my artifactory under the same version.
is it possible? and if so what should I add to the pom in order to do this?

You can use deploy:deploy-file goal from Apache Maven Deploy Plugin
<plugins>
<!-- other plugins ...-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
<executions>
<execution>
<goals>
<goal>deploy-file</goal>
</goals>
<phase>deploy</phase> <!-- change this if you need another phase -->
<configuration>
<groupId>${project.groupId}-script</groupId><!-- whatever you want -->
<artifactId>${project.groupId}</artifactId><!-- same groupId as the project, you can choose another one-->
<version>${project.version}</version><!-- same version as the project, but you can choose another one -->
<file><!-- path to your script --></file>
<pomFile> <!-- path to the pom file of the script --></pomFile>
<generatePom><!-- but maybe you don't want a pom file for the script, so set this to false--></generatePom>
<repositoryId><!-- the one configured in your settings.xml and distributionManagement --></repositoryId>
<url><!-- the one configured in your settings.xml and distributionManagement --></url>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

Related

How to spring-boot:repacakge to Jar and War from a codebase with single maven command

I want to generate both Jar and War spring-boot packages from the same codebase via single maven command mvn install
We are currently generating individual Jar and War spring-boot packages from the same codebase with different profiles in pom via mvn install -PJar && mvn install -PWar,
Currently i'm able to generate Jar and War packages by packaging war. The flow is like below.
Compile - > Package Jar -> Package War -> Repackage Jar to spring-boot Jar.
I'm unable to achieve the next step Repackage War to spring-boot War
<!-- Jar is the default package -->
<!-- Package War -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>package-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Spring Boot -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
The above is creating Jar and War packages and only spring-boot Jar package, spring-boot War package is not created.
i have tried using classifiers in execution configuration to bind the war packaging with war repackaging in spring-boot, But the repackage looks for ${artifact-id}-${classifier}.jar instead of ${artifact-id}-${classifier}.war and skips repackaging of war.

Maven: how to query the executable classpath?

I have a maven project with some specified dependencies.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
How can I query maven to find out the path it's using for these dependencies, or the classpath I should use for independent execution?
My goal is to build a wrapper which runs the program with the appropriate classpath.
Several alternatives are available in Maven:
Maven Dependency Plugin (build-classpath goal)
Look at the Maven Dependency Plugin, especially the build-classpath goal provides exactly the full classpath for external execution usages. Among many options, The outputFile parameter may be helpful.
You don't need to configure it for usage, just run
mvn dependency:build-classpath
On your project and you'll see the classpath as part of the build output. Or
mvn dependency:build-classpath -Dmdep.outputFile=classpath.txt
To redirect just the classpath to a file.
Maven Dependency Plugin (copy-dependencies goal)
To build a wrapper, you could also look at the copy-dependencies goal, which would copy the required dependencies (jars), including transitive dependencies, to a configured folder (so you don't need hardcoded paths to your local machine).
An example of plugin configuration is available on the official site, here.
For instance, the following configuration:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dependencies</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Would add to the folder target/dependencies all the dependencies declared in scope compile. NOTE: with respect to the linked official example, I added the <includeScope>runtime</includeScope> configuration entry (which will include compile and runtime scoped dependencies, according to documentation and my tests), otherwise it would also include the test scope by default (which is something I believe you would not need at runtime).
Exec Maven Plugin (java or exec goals)
Alternatively, you can use the Exec Maven Plugin to execute a main from Maven using the required classpath.
An example of plugin configuration is available on the official site, here.
The following configuration for instance:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>my-execution</id>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.sample.MainApp</mainClass>
</configuration>
</plugin>
Will configure the Exec plugin to run via mvn exec:java the main class MainApp as configured, obviously with the required classpath.
Maven Assembly Plugin
Lastly, the Maven Assembly Plugin also provides facilities to build an executable jar with dependencies, as explained here, in another question on stackoverflow.

Run Maven goal for only one module in multi module project

I have a project which has 4 modules. That said, I have a parent project with a parent POM.xml specifying theses modules.
<modules>
<module>Module1-Desktop</module>
<module>Module2-Core</module>
<module>Module3-Web</module>
<module>Module4-EAR</module>
</modules>
The Module4-EAR has Module3-Web and Module2-Core as dependencies (not as modules) and generates an EAR to be deployed to JBoss.
I want to be able to build the Module4-EAR, but building Module3-Web and Module2-Core before it, so it is up-to-date and also deploy it to JBoss. So I'm running the following Maven command in the parent project:
mvn --projects Module2-Core,Module3-Web,Module4-EAR clean install jboss:hard-undeploy jboss:hard-deploy
In the parent POM I've set the jboss-maven-plugin like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
In the Module4-EAR's pom.xml I've set the jboss-maven-plugin this way:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<jbossHome>${env.JBOSS_HOME}</jbossHome>
<serverName>default</serverName>
<unpack>true</unpack>
<fileNames>
<fileName>build/Module4-EAR.ear</fileName>
</fileNames>
</configuration>
</plugin>
The problem is that I want only the Module4-EAR.ear to be deployed with to the server, but the Module3-Web.war and Module2-Core are deployed as well. I want to do it in a single command, instead of running 2 mvn commands, one to "clean install" the projects and another to deploy only the EAR. How can I achieve that?
If you only want to deploy the ear then add the jboss-maven-plugin into the pom of Module4-EAR. So the plugin will only work within that module. Adding deployment plugins into a parent pom often leads to executing it into all modules - which is often not what you want.
You can chain the commands so a mvn clean install plugin:goal
The plugin you use seems to be superceded by JBoss AS7 Deployment Plugin: http://docs.jboss.org/jbossas/7/plugins/maven/latest/ (as a side note)
So it would look like mvn clean install jboss-as:deploy
You can also add an execution to the jboss plugin and bind it to the deploy phase so a mvn clean deploy would copy the artifact into a remote repository and deploy the ear into jboss.
An example using the install phase is at: http://docs.jboss.org/jbossas/7/plugins/maven/latest/examples/deployment-example.html
Just had to add this to the only project that I want to execute the jboss:hard-deploy.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<jbossHome>${env.JBOSS_HOME}</jbossHome>
<serverName>default</serverName>
<unpack>true</unpack>
<fileNames>
<fileName>build/cadprev-web-ear-1.0.0.ear</fileName>
</fileNames>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>hard-deploy</goal>
</goals>
</execution>
</executions>
</plugin>
Removed the code below from the parent POM.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
And changed the mvn command to:
mvn --projects Module2-Core,Module3-Web,Module4-EAR clean install

Multiple install:install-file in a single pom.xml

(Please read at least this before answering: This is a temporary measure! No, we do not want to set up a local repository manager and manually run a script)
We have a legacy project with a few dependencies which we have a local copy of including source and javadoc, and which has been proven to work well in production, but which is not available in the same quality in Central. We want to use those jars we already have.
I have found that I can manually run a suitably complex mvn install:install-file command to get the artifacts injected in the repository of the local machine, but I would like to have it work as part of the normal maven build of our various modules.
Given I have an otherwise blank module containing multiple jars which each need to be inserted with an install:install-file how should I do this in my pom.xml to be fully conformant with the normal Maven build?
Or can I just attach multiple jars to be the output of the module and somehow attach javadoc and source too)?
(and, please, no suggestion about submitting to central or setting up a local repository manager. This is a temporary solution until we have an opportunity to upgrade to a newer version of the dependencies)
I would imagine something like this would work (this will install it on every build):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>inst_1</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<!-- config for file 1 -->
</configuration>
</execution>
<execution>
<id>inst_2</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<!-- config for file 2 -->
</configuration>
</execution>
<!-- execution file 3... -->
</executions>
</plugin>
</plugins>
</build>

How to deploy the sources file with the jar using deploy:deploy-file

I have the following plugins for creating a -sources.jar and deploying a specific named jar to a repository.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.5</version>
<configuration>
<version>${project.version}-r${buildNumber}</version>
<classifier>${env}</classifier>
<packaging>jar</packaging>
<file>${project.build.directory}/${project.build.finalName}.jar</file>
<url>${artifactory.url}/libs-release-local</url>
<repositoryId>artifactory.digiterre.com</repositoryId>
<pomFile>${project.basedir}/pom.xml</pomFile>
</configuration>
</plugin>
I wish to deploy the *-sources.jar at the same time. I have tried adding a second file entry and even a second deploy plugin. I seem to get one or other file deployed.
Is it possible to deploy both in one pass using deploy:deploy-file or will I have to set up a second team city build just to deploy the sources?
When you use maven-source-plugin, the generated jar will automatically attach to project artifact (default setting for this parameter is 'true') and if you execute deploy it will be deployed along with it. Alas, no need for separate configuration of deploy plugin.
Unfortunately, you cannot add classifier (${env} in your case) to sources jar. That is why I'd use the following configuration:
...
<artifactId>com.pie.mash.repo.mince-${env}</artifactId>
<version>1.18-r${buildNumber}</version>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Also, I've found this question on SO. You can use the workaround suggested there.
We can use deploy:deploy-file to upload multiple JARs (sources, tests, docs) along side main JAR artifact. We just need to supply that additional piece of information to deploy:deploy-file plugin call. The additions are indicated in bold in below command:
mvn deploy:deploy-file
-Dfile=helloWorld.jar
-Durl=https://localhost/nexus/content/repositories/snapshots/
-DrepositoryId=snapshot
-Dfiles=helloWorld-6.4.1.3.SNAPSHOT-sources.jar,helloWorld-6.4.1.3.SNAPSHOT-tests.jar
-Dtypes=jar,jar -Dclassifiers=sources,tests
-DgroupId=com
-DartifactId=helloWorld
-Dversion=6.4.1.3.SNAPSHOT
-Dpackaging=jar
-Dpomfile=pom.xml
We need to specify list of files separated by commas.
We need to specify the types of those additional files.
We need to add classifier information for those additional files.
mvn deploy:deploy-file only deploys a single artifact. Instead you can use mvn deploy (which invokes mvn deploy:deploy) to deploy the artifact, its pom along with the attached artifacts (like source and javadoc). Refer to the goals overview of maven deploy plugin.

Categories

Resources