使用maven创建可执行jar

时间:2013-08-21 14:14:42

标签: java maven executable-jar

我正在尝试使用maven创建一个可执行jar。我有一个java web项目,我使用m2e eclipse插件转换为maven。为了创建一个可运行的jar,我的pom.xml按以下方式配置。

<build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>

    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>

    <!--  to create a runnable jar -->  
      <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.test.main</mainClass> // main being main.java which has my main method
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>

    </plugins>
  </build>

然后我做了编辑 - >运行as-&gt;在(编辑配置)选项卡中,我将“包”添加到目标选项卡。该项目成功构建,但当我尝试运行jar时,我一直“找不到主要清单”,我哪里出错了?

1 个答案:

答案 0 :(得分:1)

我认为您错过了将插件绑定到生命周期阶段:

http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Execution:_Building_an_Assembly

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
             <manifest>
                <mainClass>com.test.main</mainClass> // main being main.java which has my main method
             </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      [...]
</project>