使用maven的依赖关系的可执行jar

时间:2014-08-01 13:34:05

标签: java maven executable-jar

我通过更改我的pom文件n次尝试了来自网络上的大量帖子。 我仍然无法创建具有所有依赖项的可执行jar。最后一个jar缺少主类。但是,我可以在Manifest.mf文件中看到相同的内容。以下是我的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sample-service</groupId>
<artifactId>sample-service</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>

<dependencies>

    <dependency>
        <groupId>io.undertow</groupId>
        <artifactId>undertow-core</artifactId>
        <version>1.0.15.Final</version>
    </dependency>
    <dependency>
        <groupId>com.ning</groupId>
        <artifactId>async-http-client</artifactId>
        <version>1.7.5</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.6</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.6</version>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.5.1</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>
<build>
    <finalName>sample-service</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.1</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.sample.app.SomeMainServer</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <source>1.7.0_60</source>
                <target>1.7.0_60</target>
            </configuration>
        </plugin>
    </plugins>
</build>

尝试了所有这些mvn package mvn clean install mvn clean compile assembly:single

但没有运气,主要类不包含在jar中。 以下是我的manifest.mf

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: userName
Build-Jdk: 1.7.0_60
Main-Class: com.sample.app.SomeMainServer

1 个答案:

答案 0 :(得分:0)

根据评论中的要求,我发布了这个答案:

jar是使用maven-jar-plugin构建的,所以你可以根据文档指定它的主类:http://maven.apache.org/shared/maven-archiver/examples/classpath.html