1

I have two java maven projects - abc in eclipse and xyz in intellij idea. They have some common jar dependencies.

In xyz code, I need to call some code in test directory of abc. So, I converted abc into a jar and imported it inside xyz. The xyz code which calls abc methods compiles fine, but fails because jars needed by abc could not be found. Some of these missing jars are actually present in xyz, but some are not.

How do I make xyz import all the dependencies needed by abc ? Is this an ok way to reuse code ?

MasterJoe
  • 1,280
  • 3
  • 17
  • 46
  • How did you imported abc JAR? The correct way would be to install abc package to local Maven repo (`mvn install`), then add it to xyz's dependencies. – juzraai Jul 10 '18 at 21:51
  • @juzraai - I did *not* add abc jar to local m2 repo. I added it to some random folder and then added it to the project. How do I add abc to the local m2 repo ? – MasterJoe Jul 10 '18 at 22:02
  • Step into abc directory and call `mvn install` from terminal. – juzraai Jul 10 '18 at 22:03

2 Answers2

1

The correct way of doing this:

  1. Install "abc" project into your local Maven repository by running mvn install inside "abc" folder. (This will build the project first, then copy the JAR and the POM into your local Maven repository.)
  2. And adding it as a dependency in "xyz" project:
<dependencies>
  <dependency>
    <groupId>...</groupId>
    <artifactId>abc</artifactId>
    <version>...</version>
  </dependency>
</dependencies>

This way, "xyz" project will have "abc" and "abc"'s dependecies too on the classpath.

Edit: reusing test classes can be done this way:

  1. Add maven-jar-plugin to build plugins with test-jar goal in "abc", which creates a JAR of your test classes:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                <goals>
                    <goal>test-jar</goal>
                </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. Install "abc" (will install regular JAR, test JAR and POM)
  2. Add as dependency and reference the test-jar classifier:
<dependencies>
    <dependency>
      <groupId>...</groupId>
      <artifactId>abc</artifactId>
      <version>...</version>
      <type>test-jar</type>
      <scope>test</scope>
    </dependency>
</dependencies>
juzraai
  • 4,835
  • 8
  • 26
  • 41
  • Thanks. The install is running now. Btw, will this jar also have the test directory of abc ? xyz needs to use the classes in abc's test directory. – MasterJoe Jul 10 '18 at 22:14
  • Nope, but I added steps for that case, please see my edited answer. – juzraai Jul 10 '18 at 22:25
  • I am so sorry, but I forgot to mention this. The test classes use some (not all) classes of main directory (POJOs). Would this info change your answer ? – MasterJoe Jul 10 '18 at 22:31
  • How can I skip running all unit and functional tests ? Its taking forever to run the functional tests. – MasterJoe Jul 11 '18 at 05:17
  • I think then you should add the normal JAR and the test JAR too to xyz's dependencies. You can skip tests by adding `-DskipTests` argument to the Maven command. – juzraai Jul 11 '18 at 07:23
  • After mvn install -DskipTests (with test-jar goal in pom), I see two jars, 1 for the abc code only and the other for abc-tests. I'll import these into xyz. – MasterJoe Jul 11 '18 at 15:50
  • This solution worked well for me. Now I have this new issue - https://stackoverflow.com/questions/14024756/slf4j-class-path-contains-multiple-slf4j-bindings#. I wonder which answer is the best way to fix it. – MasterJoe Jul 11 '18 at 17:44
  • I think [adding exclusions](https://stackoverflow.com/a/22638585/2418224) to the dependencies would help. – juzraai Jul 11 '18 at 19:37
  • small question. how do I also make a sources jar for the test jar and main jar ? – MasterJoe Jul 13 '18 at 04:17
-1

There are a few more ways.

  • If abc and def projects, have some what similar functionality, you can group them together in modules and make a parent-pom in your project, and configure the two projects as modules, thus effectively sharing all the dependencies in one place, ie. the parent pom, this also helps in keeping the versions of all other dependencies consistent. Check this link to see how that can be done

    https://books.sonatype.com/mvnex-book/reference/multimodule-sect-simple-parent.html

  • On the other hand, if abc project only needs def as a dependency for abc's tests, then you can import def as a test scope dependency. Which means its only available in test scope. What that means is that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases, also its not transitive. So, something like this:-

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    
MithunS
  • 456
  • 4
  • 26