mvn exec:java没有正确设置classpath

时间:2015-01-26 11:01:15

标签: java maven

我试图在命令行上使用Maven运行Java程序,但它没有在类路径上放置正确的条目。如果我在IntelliJ(具有Maven支持)中运行程序,则类路径有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>

2 个答案:

答案 0 :(得分:1)

默认情况下,exec:java使用&#39;运行时&#39;范围,不会使用&#39;编译&#39;设置依赖关系。范围。

您可以使用:

exec:java -Dexec.classpathScope="compile"

包含编译依赖项(不是100%确定-D语法,但变量是exec.classpathScope肯定)。

这应该可以解决问题。 如果您需要更多信息/选项,插件页面会列出一些选项:http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html

答案 1 :(得分: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
....

您应该会看到许多“添加项目依赖项工件”消息。

相关问题