我正在从头开始创建Maven项目,首先只是学习如何使用Maven。
主项目不包含源代码文件,但是它具有两个模块(app和util)。应用取决于util。我想从应用程序中获取一个.jar,并且如果可能的话没有清单文件。
我可以使用mvn clean install编译整个项目,但无法从控制台运行该应用程序。

主要项目.pom是

<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.proba</groupId>
  <artifactId>myproject</artifactId>
  <version>0.0.1</version>
<packaging>pom</packaging>


<modules>
  <module>util</module>
  <module>app</module>
</modules>
</project>


Util .pom是

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.proba</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1</version>
  </parent>
  <artifactId>util</artifactId>


 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
     </dependency>
 </dependencies>

</project>


app pom是

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.proba</groupId>
        <artifactId>myproject</artifactId>
        <version>0.0.1</version>
    </parent>
    <artifactId>app</artifactId>
    <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>com.proba</groupId>
            <artifactId>util</artifactId>
            <version>0.0.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.proba.app.App</mainClass>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.1.1</version>
                <executions>
                    <execution>
                        <phase>site-deploy</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>com.proba.app.App</mainClass>
                            <!--<arguments> <argument>myArg1</argument> <argument>myArg2</argument>
                                </arguments> -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


该代码并没有做太多。
App.java(在app模块内部)是
    包com.proba.app;

import com.proba.util.UtilClass;
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        UtilClass uc = new UtilClass();
        System.out.println( "QQQQQ: " + uc.print() );
    }
}


我用它编译

mvn clean install
[INFO] Reactor Summary:
[INFO]
[INFO] myproject .......................................... SUCCESS [  0.406 s]
[INFO] util ............................................... SUCCESS [  2.074 s]
[INFO] app ................................................ SUCCESS [  0.535 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------


myproject,util,app成功。

我尝试用

java -jar app/target/app-0.0.1.jar
Hello World!
Exception in thread "main" java.lang.NoClassDefFoundError: com/proba/util/UtilClass
        at com.proba.app.App.main(App.java:17)
Caused by: java.lang.ClassNotFoundException: com.proba.util.UtilClass
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more


如您所见,Hello World!打印出来,但未找到UtilClass。
我可以从Eclipse运行构建,但不能从控制台运行。
我做错了什么?你有什么主意吗?

最佳答案

您缺少的是一罐UtilClass。您只需要在pom中添加阴影插件即可。
作为参考:maven-shade-plugin

10-05 21:20
查看更多