我有两个项目,并希望将一个项目与另一个项目之间的依赖关系链接起来。
在我的第一个项目中,带有以下pom.xml文件:
<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>
在我的第二个项目中,我想重用已经在第一个项目中定义的opencsv依赖关系,所以我在第二个项目的pom文件中做了以下操作:
<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>
在第二个项目的实现中,我仍然可以在opencsv库中调用方法,但是当我运行它时,它引发了NoClassDefFoundError:
线程“ pool-1-thread-1”中的异常java.lang.NoClassDefFoundError: com / opencsv / CSVWriter com.customer.project2(ExportCommonExtension.java:41) 在com.customer.project1.doExportCommon(ExportJob.java:95) 在com.customer.project1.executeTask(ExportJob.java:55) 在com.customer.project1.StartWork.run(Job.java:135)处 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) 在java.lang.Thread.run(Thread.java:748)
原因: java.lang.ClassNotFoundException:com.opencsv.CSVWriter位于 java.net.URLClassLoader.findClass(URLClassLoader.java:381)在 java.lang.ClassLoader.loadClass(ClassLoader.java:424)在 sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:338)在 java.lang.ClassLoader.loadClass(ClassLoader.java:357)在 org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:151) 在java.lang.ClassLoader.loadClass(ClassLoader.java:357)...还有7个
我是否错过了pom xml文件中的某些内容?有谁知道如何解决它?谢谢你!