生成一个可运行的jar并在其中包含Maven中的库

时间:2014-10-23 11:03:58

标签: maven jar dependencies pom.xml runnable

我正在尝试使用Maven和Eclipse编译Java项目,但我尝试了很多在网络上看到的解决方案,但它们似乎都没有用。我只想构建应用程序,创建一个可运行的jar并包含所需的库。我尝试了maven依赖或maven-assembly但我肯定会错过一些因为我每次都失败了。

这是我的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>

    <!-- Project Description -->
    <groupId>org.awax</groupId>
    <artifactId>toolbox</artifactId>
    <version>0.1.0</version>
    <name>ToolBox</name>
    <packaging>jar</packaging>
    <url>http://maven.apache.org</url>
    <description>Custom library containing useful code</description>

    <!-- Project Properties -->
    <properties>
        <jdk.version>1.7</jdk.version>
        <!-- Libraries Version -->
        <log4j.version>1.2.17</log4j.version>
        <jdom.version>2.0.5</jdom.version>
        <miglayout.version>3.7.4</miglayout.version>
        <jfreechart.version>1.0.19</jfreechart.version>
        <bounce.version>0.18</bounce.version>
        <!-- Maven Plugins Version -->
        <eclipse.version>2.9</eclipse.version>
        <compiler.version>3.2</compiler.version>
        <jar.version>2.5</jar.version>
        <assembly.version>2.4.1</assembly.version>
        <dependency.version>2.5.1</dependency.version>
    </properties>

    <!-- Project Libraries -->
    <dependencies>
        <!-- LOG4J -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!-- Jdom -->
        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom2</artifactId>
            <version>${jdom.version}</version>
        </dependency>
        <!-- MigLayout -->
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout</artifactId>
            <version>${miglayout.version}</version>
        </dependency>
        <!-- JFreeChart -->
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>${jfreechart.version}</version>
        </dependency>
        <!-- Bounce -->
        <dependency>
            <groupId>org.bounce</groupId>
            <artifactId>bounce</artifactId>
            <version>${bounce.version}</version>
        </dependency>
    </dependencies>

    <!-- Build Options -->
    <build>
        <pluginManagement>
            <plugins>
                <!-- Attach source code and Javadoc -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <version>${eclipse.version}</version>
                    <configuration>
                        <downloadSources>true</downloadSources>
                        <downloadJavadocs>false</downloadJavadocs>
                    </configuration>
                </plugin>
                <!-- Set a JDK compiler level -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${compiler.version}</version>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                    </configuration>
                </plugin>
                <!-- Copy project dependencies -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>${dependency.version}</version>
                    <executions>
                        <execution>
                            <id>copy-dependencies</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <includeScope>provided</includeScope>
                                <outputDirectory>${project.build.directory}/dependency</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <!-- Create the Jar -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>${jar.version}</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <!-- Jar file entry point -->
                                <mainClass>${project.groupId}.${project.artifactId}.tools.WaveformGenerator</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <!-- Add dependencies to generated Jar -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>${assembly.version}</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

1 个答案:

答案 0 :(得分:6)

使用maven-assembly-plugin生成带有依赖项的可执行jar

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <groupId>org.apache.maven.plugins</groupId>
    <version>2.4.1</version>
    <executions>
        <execution>
            <id>make-executable-jar-with-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.coderplus.sample.MainClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

它不适合您,因为您已在pluginManagement部分添加了插件执行。 pluginManagement应该用于管理插件版本和配置。

构建代码的最终版本应如下所示:

<build>
<!-- if you have a multimodule project, I will probably manage this in the
    parent pom -->
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>${assembly.version}</version>
      </plugin>
    </plugins>
  </pluginManagement>

  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <groupId>org.apache.maven.plugins</groupId>
      <executions>
        <execution>
          <id>make-executable-jar-with-dependencies</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <archive>
              <manifest>
               <addClasspath>true</addClasspath>
               <mainClass>com.coderplus.sample.MainClass</mainClass>
             </manifest>
            </archive>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
      </execution>
    </executions>
  </plugin>
  </plugins>
</build>