2

I have two projects and would like to link dependency between one project to another.

In my first project with following pom.xml file:

    <artifactId>project1</artifactId>
    <name>Project 1</name>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>4.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>${maven-antrun-plugin.version}</version>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <copy todir="${basedir}/../bin">
                                    <fileset dir="${basedir}/target/">
                                        <include name="*.jar" />
                                    </fileset>
                                </copy>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

And in my second project I would like to reuse the opencsv dependency which already defined in the first project so I did following in the pom file of my second project:

    <artifactId>project2</artifactId>
    <name>Project 2</name>
    <packaging>jar</packaging>

    <properties>
        <parent-job-artifactId>project1</parent-job-artifactId>
        <parent-job.version>1.0.0-SNAPSHOT</parent-job.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.customer</groupId>
            <artifactId>${parent-job-artifactId}</artifactId>
            <version>${parent-job.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>${parent-job-artifactId}-${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>${maven-antrun-plugin.version}</version>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <copy todir="${basedir}/../bin">
                                    <fileset dir="${basedir}/target/">
                                        <include name="*.jar" />
                                    </fileset>
                                </copy>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

In the implementation of the second project, I can still call the methods in opencsv library but when I run it, it threw NoClassDefFoundError:

Exception in thread "pool-1-thread-1" java.lang.NoClassDefFoundError: com/opencsv/CSVWriter at com.customer.project2(ExportCommonExtension.java:41) at com.customer.project1.doExportCommon(ExportJob.java:95) at com.customer.project1.executeTask(ExportJob.java:55) at com.customer.project1.StartWork.run(Job.java:135) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)

Caused by: java.lang.ClassNotFoundException: com.opencsv.CSVWriter at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:151) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 7 more

Did I miss something in my pom xml file? Does anyone has an idea how to fix it? Thank you in advanced!

Bali
  • 705
  • 4
  • 13
  • 21
  • This seems to be the same problem as https://stackoverflow.com/questions/43999612/maven-transitive-dependencies-are-not-resolved-for-artifact-deployed-on-artifa as the dependency you're looking to get in the second project is a transitive dependency from the first. I'd suggest checking `mvn dependency:tree` for the second. Or you could maybe avoid the problem by making project 1 the parent or creating a common parent pom that is used by both projects like in https://howtodoinjava.com/maven/maven-parent-child-pom-example/. – Ryan Dawson Aug 14 '18 at 10:49
  • hi, actually I have a common parent project for project 1 and project 2 already. This common project is the parent project for not only project 1 and project 2 but also for project 3, project 4.... in my application. But because this opencsv dependency I need only for project 1 and project 2 so I did like above. But anyway, even when I put the dependency in my parent project, I still got this exception, don't know why – Bali Aug 14 '18 at 11:51
  • Do you see it in `mvn dependency:tree` for project 2? – Ryan Dawson Aug 14 '18 at 11:57
  • yes I saw it: the tree looks like this: \- com.opencsv:opencsv:jar:4.1:compile +- org.apache.commons:commons-lang3:jar:3.6:compile +- org.apache.commons:commons-text:jar:1.1:compile \- commons-beanutils:commons-beanutils:jar:1.9.3:compile \- commons-collections:commons-collections:jar:3.2.2:compile – Bali Aug 14 '18 at 12:22
  • Does it build when you do `mvn clean install` from the directory of the parent project? It could be an IDE integration issue (I'm guessing eclipse?). – Ryan Dawson Aug 14 '18 at 12:27
  • yes it build successfully, when I was coding it also compiled successfully. I got this error when I build the jar file for project 1 and project 2 and run them – Bali Aug 14 '18 at 12:31
  • so the error only happens in eclipse? – Ryan Dawson Aug 14 '18 at 12:33
  • no it happened even when I run in command outside of eclipse – Bali Aug 14 '18 at 12:36
  • So very similar to https://stackoverflow.com/questions/10568275/noclassdeffounderror-on-maven-dependency or https://stackoverflow.com/questions/34055029/noclassdeffounderror-spring-boot-maven Maybe try some of the suggestions from those threads – Ryan Dawson Aug 14 '18 at 12:46
  • thank you, I will have a look on them – Bali Aug 14 '18 at 13:58

0 Answers0