Java Maven exec插件类NotFoundException
我正在使用Maven exec插件从命令行使用mvn exec:java命令运行java应用程序。我已经在pom.xml中指定了主类和相关的依赖项Java Maven exec插件类NotFoundException,java,maven,Java,Maven,我正在使用Maven exec插件从命令行使用mvn exec:java命令运行java应用程序。我已经在pom.xml中指定了主类和相关的依赖项 <groupId>com.example.MyApp</groupId> <artifactId>MyApp</artifactId> <version>1.0.0</version> <build> <plugins> <plugin
<groupId>com.example.MyApp</groupId>
<artifactId>MyApp</artifactId>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.myclass</mainClass>
<arguments>
<argument>configFile</argument>
<argument>properties</argument>
</arguments>
</configuration>
</plugin>
我认为这是造成问题的原因,因为它在默认类路径上查找不包含依赖项的类
有什么关于我做错了什么的提示吗?您可以这样生成类路径:
<configuration>
<executable>java</executable>
<arguments>
<argument>-Dmyproperty=myvalue</argument>
<argument>-classpath</argument>
<!-- automatically creates the classpath using all project dependencies,
also adding the project build directory -->
<classpath/>
<argument>com.example.Main</argument>
...
</arguments>
</configuration>
JAVA
-Dmyproperty=myvalue
-类路径
com.example.Main
...
在从命令行执行之前,还应在命令行库中导入。您可以将此命令与您的expecific名称和库的信息一起使用:
mvn install:install-file -Dfile=MyLibrary.jar -DgroupId=com.example.MyLibrary -DartifactId=MyLibrary -Dversion=1.0.0 -Dpackaging=jar
您需要将依赖项添加为执行插件的依赖项,以便执行插件可以加载您配置的类
com.example.myclass
:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
[...]
<dependencies>
<dependency>
<groupId>com.example.MyLibrary</groupId>
<artifactId>MyLibrary</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
</plugin>
org.codehaus.mojo
execmaven插件
1.2.1
[...]
com.example.MyLibrary
我的图书馆
1.0.0
罐子
你还在寻找这个问题的答案吗?我也遇到了同样的问题,最后终于解决了
您需要向配置中添加includePluginDependencies,以使插件在您的依赖项中搜索主类:
<configuration>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>com.example.myclass</mainClass>
<arguments>
<argument>configFile</argument>
<argument>properties</argument>
</arguments>
</configuration>
真的
com.example.myclass
试试mvn clean compile exec:java
有时我们会忘记编译代码,当您在未编译的情况下运行mvn exec:java
时,显然maven找不到已编译的类文件,并抱怨ClassNotFoundException
感谢您的响应,我已将依赖项添加到插件中,但它仍然给我一个classnotfound错误。我在这里遵循了这个示例嗨,感谢您的响应,jar位于我的本地存储库alreadyHi@Moritz。您能进一步说明一下吗?我的印象是,一旦我运行了mvn exec:java,那么这个项目定义的依赖项将自动包含在类路径中……您将它放在哪里?在
?是的,这是pluginI的配置,有相同的问题,添加了includePluginDependencies,仍然是ClassNotFoundException。还有其他建议吗?同时添加includePluginDependencies,错误将继续出现。原因是“干净”的目标。在我的情况下,只要删除它,它的工作@马西武
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
[...]
<dependencies>
<dependency>
<groupId>com.example.MyLibrary</groupId>
<artifactId>MyLibrary</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
</plugin>
<configuration>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>com.example.myclass</mainClass>
<arguments>
<argument>configFile</argument>
<argument>properties</argument>
</arguments>
</configuration>