mvn exec:java无法正确设置classpath

3
我正在尝试在命令行上使用Maven运行Java程序,但它没有将正确的条目放入类路径中。如果我在具有Maven支持的IntelliJ中运行程序,则类路径包括80个左右的条目,包括我的项目的jar依赖项、编译程序类以及来自src/main/resources的资源。如果我使用 mvn exec:java 运行程序,我只得到一个条目apache-maven-3.0.4/boot/plexus-classworlds-2.4.jar,在整个项目树中没有对plexus的引用。这个条目是从哪里来的?为什么其他预期的类路径条目不在那里?
Maven版本:Apache Maven 3.0.4 (r1232337; 2012-01-17 00:44:56-0800) 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>com.example</groupId>
<artifactId>MyProject</artifactId>
<version>SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <!-- lots of dependencies -->
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <showDeprecation>true</showDeprecation>
                <showWarnings>true</showWarnings>
                <executable>${env.JAVA_HOME}/bin/javac</executable>
                <fork>true</fork>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>${basedir}/src/assembly/assembly.xml</descriptor>
                </descriptors>
            <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.1</version>
            <configuration>
                <mainClass>com.example.MyApp</mainClass>
                <executable>${env.JAVA_HOME}/bin/java</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

请展示您完整的pom文件,否则无法提供任何帮助。此外,告知您使用的Maven版本以及exec-maven-plugin的版本等信息将会更有帮助。 - khmarbaise
我已经更新了我的问题,包括pom和Maven版本,谢谢。 - user3427070
首先尝试将 exec-maven-plugin 升级到最新版本 1.3.2 - khmarbaise
3个回答

3

默认情况下,exec:java使用“runtime”范围,这将不会引入使用“compile”范围设置的依赖项。

您可以使用:

exec:java -Dexec.classpathScope="compile"

为了包含编译依赖项(关于-D语法我不是100%确定,但变量exec.classpathScope肯定是需要的)。

这应该能解决问题了。 如果你需要更多信息/选项,请查看插件页面上列出的一些选项:http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html


我尝试使用'mvn exec:java -Dexec.classpathScope="compile"'运行,但仍然遇到同样的问题。我还尝试了'-Dexec.includeProjectDependencies=true'和'-Dexec.includePluginsDependencies=true',但都没有任何效果。 - user3427070
includeProjectDependencies和includePluginsDependencies并不是必需的。然而,classpathScope是必须的,如果你还有其他问题,至少要有一个。你尝试过'mvn dependency:tree'来了解plexus依赖来自哪里吗? - Psyx

0

我不了解Plexus(我猜它是Exec Maven插件的一个依赖项?),但尝试打开调试模式运行:mvn exec:java -X,这样你的依赖项被添加到类路径中就更清晰了:

....
[DEBUG] Invoking : com.example.MyApp.main()
[DEBUG] Plugin Dependencies will be excluded.
[DEBUG] Project Dependencies will be included.
[DEBUG] Collected project artifacts [log4j:log4j:jar:1.2.16:compile, commons-lang:commons-lang:jar:2.6:compile]
[DEBUG] Collected project classpath [C:\MyProject\target\classes]
[DEBUG] Adding to classpath : file:/C:/MyProject/target/classes/
[DEBUG] Adding project dependency artifact: log4j to classpath
[DEBUG] Adding project dependency artifact: commons-lang to classpath
....

你应该会看到很多“正在添加项目依赖项”消息。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接