Maven fails dependencies - java

I cant understand, why is failing this dependency in my pom.xml:
<dependency>
<groupId>statistics</groupId>
<artifactId>statistics-1.0-SNAPSHOT</artifactId>
<version>1.0</version>
</dependency>
But I have got the root well done:
C:\m2\repository\statistics\statistics-1.0-SNAPSHOT\1.0
Any help??
Im working of course in mode offline.

Your dependency looks weird with the duplicate versionreference. Maven is looking for ${localrepository}/groupId/artifactId/version/artifactId-version.type, where the dots in the groupId (if any) are used as directory-separators.
So in you case Maven is looking for C:\m2\repository\statistics\statistics-1.0-SNAPSHOT\1.0\statistics-1.0-SNAPSHOT-1.0.jar.
However, what you probably want is
<dependency>
<groupId>statistics</groupId>
<artifactId>statistics</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

Related

Provide dependencies in the implementing app / module

I'm writing a library that I'd like to compile into implementable jar which then will be used in other projects / tests.
In my library I depend on various jars: okHttp, guava, etc., What I want to do is to tell maven not to put those dependencies into the final JAR but make that projects / modules that depend on this library provide those dependencies
How can this be done in maven?
library pom.xml
<groupId>com.example</groupId>
<artifactId>testing-library</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
implementation module pom.xml
<groupId>com.example</groupId>
<artifactId>implementation-</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>testing-library</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
But I'm getting java.lang.NoClassDefFoundError: com/google/common/base/Preconditions error
If you put code into src/test/java, this code will not be part of the final jar. The code is meant for tests during the build of the jar.
If your library is a helper library for tests, put the code into src/main/java and reference it in other projects with <scope>test</scope>.
BTW, don't use Maven shade plugin or Maven assembly plugin for a library. These are mainly meant for standalone jars that run on their own.
Ok, I solved the issue. It seems that the generated POM.xml for the testing-library did not contain any dependencies.
I was using mvn install:install-file ... -DgeneratePom=true for installing jar into local repository for quick debugging and the pom generated this way seemed to be lacking library dependencies'

Maven - Do you need a main to use a project as a library?

I'm working on a project where I have a "core" and an "API" that are two separate maven projects.
Currently, I'm exporting the API in a jar using Eclipse and I'm linking it in maven like that :
<dependency>
<groupId>com.project.my</groupId>
<artifactId>project-api</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/dependencies/project-api.jar</systemPath>
</dependency>
It's working properly and I can use the classes defined in my API.
But now I want to have a better workflow and I'm trying to install the API in my local maven repository and then link it normaly in the core :
<dependency>
<groupId>com.project.my</groupId>
<artifactId>project-api</artifactId>
<version>1.0.0</version>
</dependency>
But when I run maven clean install I'm getting the following error :
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.3.RELEASE:repackage failed: Unable to find main class
And I indeed don't have a main in my API, since I don't need one.
Note that I use spring-boot for both projects, as I'm using some of their dependencies and it's easier to let them manage the versions (I only use the utility classes in the API).
So I'd like to know if I HAVE to have a main anyway, even if I don't ever use it, or if I'm doing something wrong trying to install my API in the local repository ?
Thanks :)
Thanks to Thilo's wise advices, I managed to get it working.
So what I did was removing all unused spring-boot references from my API project and only keeping the spring dependencies I needed, and I was then able to run mvn clean install properly.
Here's what I got rid of :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
and
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath/>
</parent>
I had to add the versions to some of the dependencies that were previously managed by spring-boot, but it's now working fine.

Use differnt version of artifact downloaded by Maven

Maven download automatically dependency hierarchy for some dependency but I want to use different version in my project. What is the best way to import the right version that I need in my Java program.
I need to use parquet 1.8.1.
You should search for the dependency you like to change in your pom.xml file. You can then specify the expected version <version></version>
Have you tried exclusions?
<dependency>
....
<exclusion>
<artifactId>artifactId</artifactId>
<groupId>groupId</groupId>
</exclusion>
....
</dependency>

Unresolved Dependency in Maven

Can anyone tell me please what's wrong with my dependency Hamcrest in Maven?
That's a pity but I can't attach screenshot here.
The screenshot from IntelliJ IDEA
One the right side one can see that the dependency hamcrest-all:1.3 is inside dependency junit:4.11.
Maybe something wrong with my pom.xml file?
There is also a problem with hamcrest version 1.3 - dependency hamcrest:hamcrest-all:1.3 not found.
When I try to update Maven indices, nothing happens.
There is a slight difference between the group ids. Notice in the maven repository group id is org.hamcrest
http://mvnrepository.com/artifact/org.hamcrest/hamcrest-all/1.3
Sometimes, not specifying the dependency's version can give this Error
Unresolved Dependency
just specify the version of the dependency
example:
instead of this:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
use this:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.2</version>
</dependency>

Jena NoClassDefFoundError with Maven

I have a Java Maven project that I developed a while ago and that doesn't work anymore. It uses a parent pom together with another Maven project in which I think the Jena version was changed and it also uses an external library that uses Jena. The Maven dependency is:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
When I execute my tests I get the following errors:
java.lang.NoClassDefFoundError: Could not initialize class
com.hp.hpl.jena.query.ARQ
java.lang.NoClassDefFoundError: org/apache/jena/iri/IRIFactory
at org.openjena.riot.system.PrefixMap.add(PrefixMap.java:54)
at com.hp.hpl.jena.sparql.util.MappingRegistry.addPrefixMapping(MappingRegistry.java:33)
at com.hp.hpl.jena.query.ARQ.init(ARQ.java:449) [...]
The errors are not thrown by my code directly but by the library I include. Can I prevent this by downgrading the Jena version in the parent pom or what can I do here?
P.S.: I now have a minimal code example that reproduces the error (java.lang.NoClassDefFoundError: org/apache/jena/iri/IRIFactory):
import org.junit.Test;
import com.hp.hpl.jena.query.ARQ;
public class DependencyTest
{
#Test
public void testARQ()
{
ARQ a = new ARQ();
}
}
And I guess it comes from this dependency:
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.1-incubating-SNAPSHOT</version>
</dependency>
I know there is probably a factory instead of a constructor but I guess this still shows where the problem is.
P.S.: I noticed that I had the dependencies "jena", "arq" and "jena-arq":
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.1-incubating-SNAPSHOT</version>
</dependency>
dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
So I thought maybe I have too much overlapping dependencies and commented out "jena" and "arq". But I still get the error
java.lang.NoClassDefFoundError: Could not initialize class com.hp.hpl.jena.query.ARQ
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.<init> [...]
I also tried out forcing a non-snapshot-version 2.9.0-incubating, but then I still get the NoClassDefFoundError with and without using the "jena" and "arq"-dependencies.
P.P.S.:
I still get the same error even when I use the following dependencies:
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.7.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-iri</artifactId>
<version>2.7.0-incubating</version>
</dependency>
You can search for the missing class using the Maven Central search application
http://search.maven.org/#search|ga|1|fc%3A%22com.hp.hpl.jena.query.ARQ%22
It demonstrates that the dependency you appear to be missing is:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.6.0</version>
</dependency>
Doesn't appear to be a version 2.6.4, but you're probabily best advised to go for a more modern version (This project was recently donated to apache)
Instructions for using Apache Jena with Maven are here:
incubator.apache.org/jena/download/maven.html
Specifying ARQ 2.9.0 as a dependency in your project pom.xml will pull in the other Jena components that you need.
I finally resolved this error by excluding the "jena"-Dependency brought in as a transitive dependency from some library.

Categories