Maven: Get repository URL of a dependency - maven

I want do display the URL to a JAR that was deployed to our maven repo at the end of my build job. (Basically the "link" where the dependency - the JAR - can be downloaded from the repository server)
So how to display the remote repository URL of a dependency on command line?

I suggest you to compose the URL from the parameters in the very pom. Example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>deploy-message</id>
<phase>deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>deploying to url=${project.distributionManagement.repository.url}/${project.groupId}/${project.artifactId}/${project.version}/${project.artifactId}-${project.version}.jar</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
(This message will appear at the same time the deploy is being performed, but you can set it to whatever phase you want. Or include it into a Maven profile.)

Related

How to check if dependency has snapshot using Github action

I have a maven project. I am replacing maven-release-plugin with maven ci friendly feature. But I want to check if any of the dependency in pom has snapshot version. If so want to fail the build for production. But for staging I would like to continue the build.
Is there any github action which will checks for snapshot in dependency ?
Answering my own question. #khmarbaise was right. I did using following setup
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>initialize</phase>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireReleaseDeps>
<message>No Snapshots Allowed for Dependencies!</message>
</requireReleaseDeps>
<requireReleaseVersion>
<message>Snapshot Version is Not Allowed for Release</message>
</requireReleaseVersion>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>

maven jar deployed twice in a war module with attachClasses set to true

I am trying to use module as a dependency in another project by deploying it to our nexus repository.
The problem is that maven tries to deploy the jar twice and our policy forbids to overwrite a release version.
The module is packaged as a war.
Here is my configuration.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>build-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
<configuration>
<attachClasses>true</attachClasses>
<failOnMissingWebXml>true</failOnMissingWebXml>
</configuration>
</plugin>
When I set attachClasses to false, everything goes fine and the war gets deployed, but the jar doesn't.
When I set it to true, the jar gets deployed but maven tries to deploy a second time and then the build fails.
Any idea why maven tries to deploy it twice.
(The module has a parent module, but it does not depend on any other module from the project).
The problem is probably that you defined your own execution.
Try to remove the block
<executions>
<execution>
<id>build-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>

Why does maven-release-plugin uploads build information? And can it be removed?

When using the maven-release-plugin to release an artifact onto a repository, the entire pom is copied. This includes sections build and reporting.
I can understand that deployement information is propagated since dependencies of a project by the same creators are likely to be deployed on the same servers, but, for non-pom artifact, I don't understand the point of having the build information.
Is it possible to create a release stripped of this information?
Use the flatten-maven-plugin
https://www.mojohaus.org/flatten-maven-plugin/
I copied the relevant plugin configuration from the website above.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<!--<version>1.1.0</version>-->
<configuration>
</configuration>
<executions>
<!-- enable flattening -->
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<!-- ensure proper cleanup -->
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
I strips the POM from all unnecessary information.

Maven Build some modules, liferay:deploy others

I have some modules like:
|-business jar
|-dal jar
|-model jar
|-webApp war
I need to build all jar before liferay:deploy the war.
I also have a parent pom module that allow to build all modules but it doesn't deploy my war after that.
thanks
If you have defined all modules as dependencies then maven will take care that the modules build in the proper order.
By default the Liferay deploy goal is not executed. You need execute it or add some configuration to run tit automatically. e.g.
<plugin>
<groupId>com.liferay.maven.plugins</groupId>
<artifactId>liferay-maven-plugin</artifactId>
<configuration>
<pluginType>portlet</pluginType>
</configuration>
<executions>
<execution>
<id>deploy-package</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
Does your parent pom contains all the modules in the
<modules>
<module>business-jar</module>
<module>dal-jar</module>
<module>model-jar</module>
<module>portlet</module>
<module>theme</module>
</modules>
and then make sure that your portlet/webapp war does contain
<plugin>
<groupId>com.liferay.maven.plugins</groupId>
<artifactId>liferay-maven-plugin</artifactId>
<executions>
<execution>
<id>deploy-package</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</executions>
</plugin>

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