Install maven dependency from location other than Maven Central - java

I have this 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>interos</groupId>
<artifactId>jenkins</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>cli</artifactId>
<version>2.49</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jenkins-ci.main/cli -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I run
mvn install
and I get:
[ERROR] Failed to execute goal on project jenkins: Could not resolve
dependencies for project interos:jenkins:jar:1.0: Failure to find
org.jenkins-ci.main:cli:jar:2.49 in
https://repo.maven.apache.org/maven2 was cached in the local
repository, resolution will not be reattempted until the update
interval of central has elapsed or updates are forced -> [Help 1]
http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
I tried doing this:
<repositories>
<repository>
<id>repository.maven</id>
<name>Spring Framework Maven Release Repository</name>
<url>https://mvnrepository.com</url>
</repository>
</repositories>
But then I got:
[ERROR] Failed to execute goal on project jenkins: Could not resolve
dependencies for project interos:jenkins:jar:1.0: Could not find
artifact org.jenkins-ci.main:cli:jar:2.49 in repository.maven
(https://mvnrepository.com) -> [Help 1]

You need to add jenkins repository
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>

There are 2 possible solutions (perhaps more):
Add also Maven central to the repositories section of your pom file.
Add repositories and mirrors to the Maven settings.xml file.
Option 1 and 2 can also be combined.
Another but maybe the best solution is to use your own instance of a Nexus repository manager and load all the dependencies from that Nexus instance (that Nexus should be defined as repository). That Nexus instance should synchronize with/proxy to external repositories like Maven central, Spring, ... and so on.

Related

Could not find an artifact in central when using maven build a project

I'm totally new to Maven. I am building an Apache projects using Maven command: mvn clean package.
It showed build failure and give the following error message:
[ERROR] Failed to execute goal on project taverna-perspective-myexperiment: Could not
resolve dependencies for project org.apache.taverna.workbench:taverna-perspective-
myexperiment:bundle:3.1.0-incubating-SNAPSHOT: Could not find artifact
org.jdom:com.springsource.org.jdom:jar:1.1.0 in central (https://repo.maven.apache.org/maven2) -> [Help 1]
It seems it needs a dependency org.jdom:com.springsource.org.jdom:jar:1.1.0 but it cannot find it. I searched this artifact in the internet and found a page giving the following maven configurations:
<dependency>
<groupId>org.jdom</groupId>
<artifactId>com.springsource.org.jdom</artifactId>
<version>1.1.0</version>
<scope>provided</scope>
</dependency>
I checked the pom.xml and found the dependency for org.jdom is identical with the above. What should I do to fix it? Does it mean this artifact has been removed from the central? Is there any other sources we can set for it?
in page write note:
Note: this artifact is located at Spring Plugins repository (https://repo.spring.io/plugins-release/)
then you must add https://repo.spring.io/plugins-release/ to repositories in pom.xml file:
<repositories>
<repository>
<id>spring</id>
<name>Spring Repository</name>
<url>https://repo.spring.io/plugins-release/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>com.springsource.org.jdom</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>

Cannot determine why dependency pom.xml is considered invalid

Working with proprietary source code. Some dependencies are stored in a local (e.g. on my filesystem) maven repo, which is exposed in my main project pom.xml like so:
<repository>
<!-- Embed mvn repo with non-public dependencies -->
<id>myproj.local</id>
<url>file:${basedir}/src/repo</url>
<name>MyProj Embedded Repo</name>
<releases>
<updatePolicy>always</updatePolicy>
</releases>
<snapshots>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
This worked OK until today, when I added a new jar file into the embedded repo under src/repo/com/hidden/v2/some-api/1.5.0/some-api-1.5.0.jar and a new handcrafted pom at src/repo/com/hidden/v2/some-api/1.5.0/some-api-1.5.0.pom.
Now my code builds and runs OK, but I constantly get this error from maven and cannot figure out why:
[WARNING] The POM for com.hidden.v2:some-api:jar:1.5.0 is invalid, transitive dependencies (if any) will not be available: 2 problems were encountered while building the effective model for :some-api:1.5.0
[ERROR] 'groupId' is missing. # [unknown-group-id]:some-api:1.5.0
[ERROR] 'dependencies.dependency.version' for javax.servlet:servlet-api:jar is missing. # [unknown-group-id]:some-api:1.5.0
My custom pom file seems to have the fields that are supposedly missing:
<?xml version="1.0" encoding="UTF-8"?>
<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.hidden.v2</groupId>
<artifactId>some-api</artifactId>
<version>1.5.0</version>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<!-- <scope>provided</scope> -->
<version>2.5</version>
</dependency>
</dependencies>
</project>

Maven tries to fetch shrinkwrap-bom from maven central, instead of configured repository

I am trying to build my maven project using Maven version 3.3.9 and a custom maven repository.
Building the project with Maven 3.1.0, it runs, downloads the dependencies and passes the tests.
After removing all local dependencies and upgrading Maven to 3.3.9, I get the following error while in the download phase:
failed to execute goal on project ... could not resolve dependencies ...
failed to collect dependencies at org.hibernate:hibernate-validator:5.2.4.Final:
failed to read artifact descriptor for org.hibernate:hibernate-validator:jar:5.2.4.Final:
could not transfer artifact org.jboss.shrinkwrap:shrinkwrap-bom.pom:1.2.2 from/to central (...)
Unknown host repo.maven.apache.org
Note again, that I am using a custom repository, not Maven Central.
But it works for the older version of Maven, so settings.xml is configured for my repository and the artifact does exist there.
Also, other dependencies do successfully download, it's just this dependency.
Why is Maven still trying to fetch from Maven Central regardless of my repository settings?
I had the same issue.
Our setup uses Nexus 3 repository manager. Maven Central is set up as a proxy repo in Nexus.
I have made the mistake to list central in the repositories part of settings.xml
<repositories>
...
<repository>
<id>central</id>
<url>http://<MY_NEXUS_HOST>:8081/repository/maven-central/</url>
</repository>
</repositories>
This was working for most of the dependencies. However it wasn't working for org.jboss.shrinkwrap:shrinkwrap-bom. Only this one.
The resolution was to specify Maven Central as a mirror:
<mirrors>
<mirror>
<id>central-proxy</id>
<name>central-proxy</name>
<url>http://<MY_NEXUS_HOST>:8081/repository/maven-central/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
The problem is transitive dependencies with scope import.
I solved it for hibernate-validator version 6.0.13.Final by adding the following dependencies to my pom.xml:
<dependency>
<groupId>org.jboss.shrinkwrap</groupId>
<artifactId>shrinkwrap-bom</artifactId>
<version>1.2.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-bom</artifactId>
<version>2.2.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.descriptors</groupId>
<artifactId>shrinkwrap-descriptors-bom</artifactId>
<version>2.0.0-alpha-8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Issue on the Maven bugtracker:
https://issues.apache.org/jira/browse/MNG-6397
They promise to fix it in Maven version 3.6.3.
As an addition to Sergey comment - just want to mention that given dependencies should be placed inside dependencyManagement tag
SandorRacz's advice to create a mirror worked for me as well. Thanks!!
So while building I was getting the following error in my error logs:
[ERROR] Non-resolvable import POM: Failure to transfer org.jboss.shrinkwrap:shrinkwrap-bom:pom:1.2.2 from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.jboss.shrinkwrap:shrinkwrap-bom:pom:1.2.2 from/to central (http://repo.maven.apache.org/maven2): HTTPS Required (501) # org.jboss.arquillian:arquillian-bom:1.1.10.Final,
For the sake of clarity, here is how my settings.xml looked like after creating the mirror. (I hope it saves somebody's time)
<profiles>
<profile>
<id>nexus</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<url>http://repo.maven.apache.org/maven2/</url>
<id>central-repo</id>
<layout>default</layout>
</repository>
</repositories>
</profile>
</profiles>
<mirrors>
<mirror>
<id>central-proxy</id>
<name>central-proxy</name>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
<mirrorOf>central-repo</mirrorOf>
</mirror>
</mirrors>

maven build with jenkins not picking up jar from local .m2 repository

I have a jenkins job for building my maven project(say project A) which has a dependency on a jar created by another local project(say project B). The jar is present in my local .m2 repo but Jenkins fails to find it in the local repo and goes to search it in central (https://repo.maven.apache.org/maven2) which obviously it will not find and hence my job fails. I have performed the below checks before posting this question here :
The version of the artifact in the dependent and independent projects match
The local .m2 repository of my machine has the required dependency jar version
Wrote a separate jenkins job to build project B, it built successfully and the jar is present in the .m2 repository of Jenkins home as well the path to which is /var/lib/jenkins/.m2 (In case jenkins is trying to find the jar in its own .m2 repository)
Changed the Jenkins"Global Tools configuration" for Maven installation from Manual (I had manually set up the maven home) to Automatic Installation(Version 3.3.9). NO luck, the issue remained the same.
NO change has been made to the maven settings.xml.
If I try to build Project A manually without Jenkins by using mvn clean install, it builds successfully.
So, Its only with Jenkins that I am not able to build the project.
Here is my POM for Project A
<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.akamai</groupId>
<artifactId>cobra-tracer-api</artifactId>
<version>1.0.2</version>
<packaging>war</packaging>
<name>cobra-tracer</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>cobra-tracer</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
//some dependencies here
<dependency>
<groupId>com.akamai.aps</groupId>
<artifactId>logger-service</artifactId>
<version>0.0.2</version>
</dependency>
</dependencies>
</project>
The POM for Project B 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>com.akamai.aps</groupId>
<artifactId>logger-service</artifactId>
<version>0.0.3</version>
<packaging>jar</packaging>
<name>logger-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The way you have integrated jenkins with maven is not correct. You have to use either artifactory or nexus, both of them have free versions that are sufficiently good to use. You build your project B and deploy them to above repository manager and your project A downloads it from repository manager.
For your specific problem, check
In your project configuration, private repository option is checked. This will create separate repository for each jenkins executor. If your executor in jenkins master are different for A and B, you will not see your jar files.
In your setup, you can't add any slaves. All build should run on master. I hope you are not running them in separate slaves.
I am assuming that you have run your project 'B' using mvn install. In your project 'A' run maven command using -U flag. mvn -U install.
Look at your jenkins job command line. Ensure that it is not running maven with -s flag. This is a way to pass different setting.xml. Example mvn -s /home/jenkins/setting.xml install. There is an option to define a different settings file in jenkins project configuration.
Look at your maven installation path. It should be in jenkins-workspace/tools location. Check setting.xml inside /conf. Ensure that local repository is not defined. If jenkins is installing it, then it should not be set.
Check if in your .m2 folder there is setting.xml file which has a different repo path.
Let me know how it goes.
Since you have added artifactory in your workflow now and facing plugin not found problem, add the following in your settings.xml
<mirrors>
<mirror>
<mirrorOf>*</mirrorOf>
<name>remote-repos</name>
<url>http://<artifactory-ip>:<artifactory-port>/artifactory/repo</url>
<id>remote-repos</id>
</mirror>
</mirrors>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>plugins-release</name>
<url>http://<artifactory-id>:<artifactory-port>/artifactory/plugins-release</url>
</pluginRepository>
<pluginRepository>
<snapshots />
<id>snapshots</id>
<name>plugins-snapshot</name>
<url>http://<artifactory-id>:<artifactory-port>/artifactory/plugins-snapshot</url>
</pluginRepository>
</pluginRepositories>
I am assuming that you have already added repository tag.

Maven project with PI4J library - error in opening zip file

I've got quite strange problem with pi4j library to Raspberry Pi. I have Maven project in Eclipse and just added repository and dependency for pi4j like this:
<repository>
<id>oss-snapshots-repo</id>
<name>Sonatype OSS Maven Repository</name>
<url>https://oss.sonatype.org/content/groups/public</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
I don't have any pi4j code in my project and I get this error while doing mvn install:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-shade-plugin:2.1:shade (default) on
project kosciol-main: Error creating shaded jar: error in opening zip
file
d:\Programy\apache-maven-3.1.1\m2\repositories\com\pi4j\pi4j-native\1.0-SNAPSHOT\pi4j-native-1.0-SNAPSHOT-hard-float.so
-> [Help 1]
If I delete dependency, project compiles without errors...
I tried to delete this pi4j repository from m2 folder but this didn't help.
How can I solve it?
Try to specify the scope of the dependency as "provided", like this:
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

Categories