Maven打包项目作为具有依赖性的可执行JAR

时间:2014-08-25 21:03:14

标签: java maven jar build packaging

我是Maven的新手,并且难以将我的java项目打包为可执行的Jar。当我从IDE(IntelliJ)运行它时,程序编译并正常工作。

我有以下pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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.mathanv</groupId>
    <artifactId>AlgorithmicDebugger</artifactId>
    <packaging>jar</packaging>
    <version>0.1</version>
    <name>Algorithmic Debugger</name>
    <properties>
        <jdk.version>1.6</jdk.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>net.sf.jung</groupId>
            <artifactId>jung-graph-impl</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>net.sf.jung</groupId>
            <artifactId>jung-visualization</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>1.4.2</version>
            <scope>system</scope>
            <systemPath>${java.home}/../lib/tools.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.mathanv.Main</mainClass>

                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>



</project>

目标目录如下所示:

enter image description here

然而,当我从终端运行应用程序时,我得到:

mathan:target Mathan$ java -jar AlgorithmicDebugger-0.1.jar

    Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jdi/IncompatibleThreadStateException
        at com.mathanv.Main.main(Main.java:15)
    Caused by: java.lang.ClassNotFoundException: com.sun.jdi.IncompatibleThreadStateException
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        ... 1 more

我认为可能是项目找不到tools.jar?但它在我创建的依赖文件夹中。

2 个答案:

答案 0 :(得分:0)

使用One-JAR将所有依赖项构建到一个可执行的JAR中。

答案 1 :(得分:0)

您可以使用maven shade插件。 http://maven.apache.org/plugins/maven-shade-plugin/

这将创建一个包含所有deps的jar。