我已经在StackOverflow上尝试过很多建议,并且在安装和pom.xml的许多组合中进行了3天的测试,但它们都不起作用。请帮忙。Maven Exec插件,ClassLoader ClassNotFoundException
我从一个有大量依赖项,spring-boot,hibernate等的大项目开始。然后我创建了另一个小型控制台项目,它导入并使用大项目中的一些类。他们不是父母和孩子的项目。我所做的就是将<dependency/>
添加到子项目pom中,像这样。 P.S.该大项目有一个<parent/>
spring-boot-starter-parent并使用spring-boot-maven-plugin。
<dependency>
<groupId>myGroup</groupId>
<artifactId>bigProject</artifactId>
<version>1.0.1</version>
</dependency>
这对Eclipse与m2e的作品,我只是使用“运行”Java应用程序和小项目的作品。 但是我需要在我的linux虚拟机上运行这些项目,它不使用GUI和Eclipse,只有终端。
然后经过一番阅读,我尝试使用maven exec插件来运行小型项目。我的步骤: 1.做大项目mvn clean install
,确认它出现在我的/.m2本地存储库中。 2.运行mvn compile
关于小项目 3.运行mvn exec:java
关于小项目 它在第2步失败,那些在小项目抛出包xxx中不存在的类import ...
。 maven失败,编译错误。
然后,我尝试简化问题。我创建了两个测试项目,只有1个类,myLib和HelloWorld控制台应用程序,然后添加myLib依赖项(pom)。 HelloWorld项目打印myLib包中类的消息。运行步骤1到3。
public class App
{
public static void main(String[] args)
{
//SystemLog from big project, does not work
//SystemLog log = new SystemLog();
//log.setValue("test value");
//System.out.println(log.getValue());
//CustomMessage from myLib, works fine
CustomMessage cm = new CustomMessage();
cm.setTheMessage("custom message");
System.out.println(cm.getTheMessage());
System.out.println(CustomMessage.defaultMsg);
}
}
pom.xml的小项目的
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>dependOnOthers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dependOnOthers</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>test</groupId>
<artifactId>myLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>bigproject</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>test.dependOnOthers.App</mainClass>
<!-- <arguments> <argument>argument1</argument> </arguments> -->
<!-- <arguments> <argument>-classpath</argument> <argument>target/lib/bigproject-1.0.1.jar</argument>
</arguments> -->
</configuration>
</plugin>
</plugins>
</build>
</project>
然后我加入到大项目的参考,尝试打印使用的HelloWorld日志消息,它传递第2步,MVN编译,但是当我做第3步,mvn exec:java,java.lang.ClassLoader.loadClass()
抛出ClassNotFoundException,其中我新增了一个SystemLog()实例,该类定义在大项目中。
总结一下: 目标:尝试运行使用Maven Exec插件,这对其他项目的依赖一个控制台应用程序,安装在本地回购/.m2 的SystemLog类是使用Hibernate注释的模型类。 1.小项目依赖大项目,在mvn编译失败,包不存在。 2. HelloWorld依赖于myLib,工作正常。 3. HelloWorld依赖于大项目,在运行时使用ClassNotFoundException失败。