Install artifacts from artifactory to local repository - maven

I have an artifactory which stores artifacts, this is a given pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.test2.test3</groupId>
<artifactId>web-gen</artifactId>
<version>v2.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test dynamic web layer</name>
</project>
How can I install the given artifact to my local repository?
mvn install, mvn install:file and others can't help. Pretty frustrated about it...

The simplest solution would be to use it as a dependency which will automatically download the artifact and put it into your local repository.
Otherwise you can use the maven-dependency-plugin by using the get goal:
mvn dependency:get -DartifactId=WhatEver -DgroupId=XX -Dversion=xxx -Dclassfier=..

Related

Download Maven artifact with version range

I need download specific Maven artifact using version range e.g.:
GroupId:org.apache.logging.log4j
ArtifactId=log4j-api
Version=[2.17.1,)
Right now I need it in one CI job.
How can I do it?
I was hoping to find an easier solution, but at least this works:
Create pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.vondr</groupId>
<artifactId>maven-dependency-download</artifactId>
<version>1.0.0</version>
<properties>
<dep.group>org.apache.commons</dep.group>
<dep.artifact>commons-lang3</dep.artifact>
<dep.version>3.12.0</dep.version>
<dep.type>jar</dep.type>
<dep.classifier></dep.classifier>
</properties>
<dependencies>
<dependency>
<groupId>${dep.group}</groupId>
<artifactId>${dep.artifact}</artifactId>
<version>${dep.version}</version>
<type>${dep.type}</type>
<classifier>${dep.classifier}</classifier>
</dependency>
</dependencies>
</project>
And then use it to download artifact using command:
mvn dependency:copy-dependencies "-DoutputDirectory=./downloaded-dependencies" -Ddep.group="org.apache.logging.log4j" -Ddep.artifact="log4j-api" -Ddep.version="[2.17.1,)"
Additional information
Basic description is here:
http://vondrnotes.blogspot.com/2022/09/download-maven-artifact-with-version.html
Working example is here:
https://github.com/bugs84/download-maven-dependency-with-version-range

How to configure/override Maven property used as parent version in dependencies?

Suppose there are two 3rd-party Maven artifacts with the following POM.XMLs:
artifact1 POM.XML:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>group1</groupId>
<artifactId>artifact1</artifactId>
<version>${artifact1.version}</version>
<packaging>pom</packaging>
<properties>
<artifact1.version>0.0.1-SNAPSHOT</artifact1.version>
</properties>
<modules>
<module>../artifact2</module>
</modules>
artifact2 POM.XML:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>artifact2</artifactId>
<parent>
<groupId>group1</groupId>
<artifactId>artifact1</artifactId>
<version>${artifact1.version}</version>
<relativePath>../artifact1</relativePath>
</parent>
They are built normally using
mvn clean install
Now, if I try to reuse artifact2 in a 3rd POM.XML:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>group2</groupId>
<artifactId>artifact3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>group1</groupId>
<artifactId>artifact2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
After executing...
mvn clean install
...the build fails trying to locate group1:artifact1:pom:${artifact1.version}
[ERROR] Failed to execute goal on project artifact3: Could not resolve dependencies for project group2:artifact3:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at group1:artifact2:jar:0.0.1-SNAPSHOT: Failed to read artifact descriptor for group1:artifact2:jar:0.0.1-SNAPSHOT: Could not find artifact group1:artifact1:pom:${artifact1.version} in central (https://repo.maven.apache.org/maven2) -> [Help 1]
And setting the property in the command-line doesn't work either:
mvn clean install -Dartifact1.version=0.0.1-SNAPSHOT
How can I use artifact2 as a dependency?
<parent> doesn't allow variables. Because in order to get a list of variables Maven first has to build Effective POM and for this it needs to resolve parents.
Even if there is some version of Maven that allowed such module to be installed it's not a working solution in general. Maven's ideology is that all the dependencies (including parent) could be downloaded from a repo and are not necessarily present on local FS/in the reactor. With properties in the parent dependency definition you violate this rule.

Multi Project Maven and AWS CodeBuild

I am new to Maven, and i am working on a project that have one or more dependencies on my other projects.
Example: I have 3 maven projects.
MyProject
MyProjectOther
MyProjectCommon
Both MyProject and MyProjectOther are independent of each other and do not have much to do with each other, but they both have a dependency on MyProjectCommon. (MyProjectOther dose not matter much to this question, just included it to show that MyProjectCommon is used in other projects)
Locally i can make this work, i have a workspace where i have both packages. I understand this is because Maven stores MyProjectCommon in a local repository.
Both projects (MyProject and MyProjectCommon) have their own GitHub repository, and my AWS CodeBuild is directed to build the MyProject repository. This fails as MyProjectCommon dose not exists for AWS CodeBuild.
How can i get this build to work with AWS CodeBuild, getting CodeBuild to download and build MyProjectCommon before it builds MyProject?
MyProject pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>myproject</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>myproject</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.myproject</groupId>
<artifactId>myproject.common</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
MyProjectCommon pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>myproject.common</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>myproject.common</name>
<url>http://maven.apache.org</url>
</project>
Unfortunately there is no easy way of doing this on any CI system that does not understand how underlying build dependencies are resolved and/or have 'always consistent' cached artifacts.
You do have couple of options here though. First option is to publish your artifact from the library build. You'll in this case have to setup multiple projects in CodeBuild, one for MyProject and other for MyProjectCommon. Builds for MyProjectCommon would publish artifacts to S3 (codebuild can do this) and then builds for MyProject can download the published artifact from S3 (you can also replace S3 with maven repositories but you'll have to publish the artifacts).
Other more saner option is to use maven modules to solve this issue (maven reactor) and create single repository for MyProjectCommon and MyProject. Essentially your projects end up under same repository and are built in the dependency order as specified in the pom of individual projects. Here's more documentation on the maven modules https://maven.apache.org/guides/mini/guide-multiple-modules.html
In the example you have, you'll still keep the pom file for MyProject and MyProjectCommon, but create another parent pom that defines both MyProject and MyProjectCommon as modules. You can then create CodeBuild project for this repository and run for example 'mvn install' which will compile both MyProjectCommon and MyProject in dependency order. You'll have an option to publish multiple artifacts from your build in CodeBuild or single zip with artifacts for both MyProject and MyProjectCommon.
I hope this helps.

Maven packaging: mvn deploy using .zip rather than .rar

I'm deploying from a Jenkins job, using the Maven "deploy" target, to an Archiva repository.
Currently I'm not using other targets and I create the pom.xml just in order to pack and deploy the files.
My pom.xml is
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>group_continuous</groupId>
<artifactId>MyArtifactId</artifactId>
<version>60-r12345-v0.0.01</version>
<packaging>rar</packaging>
<distributionManagement>
<repository>
<id>repo_continuous</id>
<name>Continuous</name>
<url>http://10.142.255.179:8080/repository/repo_continuous/</url>
</repository>
</distributionManagement>
</project>
In order to have it work I just put all the required files in the folder
target/MyArtifactId-60-r12345-v0.0.01/*
before calling
mvn deploy
I know that I can use for the purpose the command
mvn deploy:deploy-file
And use a plugin to just create the .zip archive but I wonder if there's a way to just add the .zip support in order to have the exact same functionality of the .rar

Where does maven take the default version of findbugs?

I'm trying to make Maven use a specific version of findbugs when running
mvn findbugs:findbugs
It seems to ignore whatever version I set inside the POM. I tried running it using just a test POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
</project>
-which just seems to use findbugs version 2.5.2 by default. Where did this version come from?
It is the latest version of the plugin. see Maven central repository
findbugs:findbugs refer to org.codehaus.mojo.findbugs:findbugs
Other refs:
MOJO Production Plugins
FindBugs Maven Plugin' project page

Resources