12

I have two projects - A and B, where A is dependent on B. I package B as jar and deploy it on a maven server (artifactory), and then include that jar as a normal dependency on project A in pom file. jar file of B shows up in the Maven Dependencies of project A, but dependencies of project B are not shown in dependency hierarchy. It is causing class not found exception for dependencies of B.

However, my project A and B and in same eclipse workspace. When I open project B, project A starts referencing the project B from the workspace instead of remote repository and everything works well.

This question - Maven. Transitive dependencies was closest to my problem, but dependencies of my project B are NOT optional.

Whats going wrong here?

POM for project B

<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myapp</groupId> 
  <artifactId>utils</artifactId>
  <version>1.0.0-RELEASE</version>
  <packaging>jar</packaging>

  <name>utils</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

<!-- Following doesn't get added to project A -->
  <dependencies>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
  </dependencies>
</project>

POM for project A

<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myapp</groupId>
  <artifactId>core-app</artifactId>
  <version>1.0.0-RELEASE</version>
  <packaging>jar</packaging>

  <name>core-app</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>com.myapp</groupId>
      <artifactId>utils</artifactId>
      <version>1.0.0-RELEASE</version>
    </dependency>
  </dependencies>
</project>

I am using maven quickstart archetype. Project structure of my projects is (which I package as jar):

project-name
  src/main/java
  src/test/java
  pom.xml
Community
  • 1
  • 1
Anmol Gupta
  • 2,797
  • 9
  • 27
  • 43
  • 1
    Your poms could help to understand the problem. – davidxxx May 16 '17 at 11:02
  • Are you getting ClassNotFoundException inside eclipse while running the application ? If yes, the project B in eclipse is resolving dependency of project A and is not being referred from Artifactory. There is an option as "Resolve workspace artifacts" that controls this. – Shailesh Pratapwar May 16 '17 at 11:07
  • @davidxxx Updated the poms, they are really this simple at this stage. – Anmol Gupta May 16 '17 at 11:12
  • @Shinchan I am getting the exception while running a junit test inside eclipse. Project B does get referred from Artifactory when I close the project B in the workspace. As soon as I open project B in workspace, it starts get referring from the workspace itself. – Anmol Gupta May 16 '17 at 11:13
  • Hmm, groupId is missing for the first pom. – davidxxx May 16 '17 at 11:14
  • @davidxxx It was a typo, sorry. – Anmol Gupta May 16 '17 at 11:18
  • So, I assume you are getting a CNFE even when project B is open in eclipse. This means, dependencies in project B are not proper in the first place. Check whether the class for which you are getting CNFE is present in B's dependency (direct and transitive both). – Shailesh Pratapwar May 16 '17 at 11:28
  • @Shinchan It works totally fine when project B is open and it resolved the dependency from the project B in workspace. – Anmol Gupta May 16 '17 at 11:29
  • Ok. Is the copy in artifactory is the latest one ? I hope you have deployed artifact B in artifactory using mvn deploy:deploy on B's existing pom and not giving a new pom and explicit jar file name. – Shailesh Pratapwar May 16 '17 at 11:31
  • @Shinchan I am using web UI of artifactory to upload jar manually. And exporting the jar from eclipse! – Anmol Gupta May 16 '17 at 12:03
  • 1
    That's the catch. Use mvn deploy:deploy. Check the maven documentation to know how to execute this command. – Shailesh Pratapwar May 16 '17 at 12:10
  • @Shinchan Yes, it did work. Thanks! I had to run maven deploy from within eclipse with dependency management tags as given by artifactory server. – Anmol Gupta May 16 '17 at 19:46

1 Answers1

8

To successfully resolve transitive dependencies, project B's jar and pom.xml must be accessible in the Maven repository. When deploying artifacts to a remote repository, be sure both the jar and pom.xml are deployed and available for download.

With the requisite files deployed to the remote repository, use the command line to build project A. Specify a build Maven target to trigger the downloading of all dependencies into the local Maven repository. Something like mvn compile or mvn package will trigger the downloads and successfully build project A.

Once, project B's jar and pom.xml are in the local Maven repository, update the Maven projects in Eclipse and they will rebuild and resolve the dependencies correctly.

Brent Worden
  • 10,624
  • 7
  • 52
  • 57
  • Thanks Brent! I am deploying pom as part of packaged jar which is accessible on maven repository (server) as part of the jar itself. Is there some other type of packaging I need to look into? – Anmol Gupta May 16 '17 at 11:28
  • @AnmolGupta Based on your input, I updated my answer to be more succinct to your projects. – Brent Worden May 16 '17 at 21:50