生成可运行的jar并使用Maven在其中包含库 [英] Generate a runnable jar and include libraries in it with Maven

查看:84
本文介绍了生成可运行的jar并使用Maven在其中包含库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to compile a Java project with Maven and Eclipse but I tried a lot of solutions seen on the web but none of them seem to work. I just want to build the application, create a runnable jar and include the needed libraries. I tried maven-dependency or maven-assembly but I surely miss something because I fail every time.

这是我的pom.xml,可以吗?或者它遗漏了什么东西?

Here is my pom.xml, is it ok or does it miss something?

<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>

推荐答案

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

Use the maven-assembly-plugin to generate an executable jar with dependencies

<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用于管理插件的版本和配置.

It's not working for you because ,you have added the plugin execution in the pluginManagement section. pluginManagement is supposed to be used for managing the plugin version and configuration.

构建标记外观的最低版本:

Minimal version of how your build tag should look like :

<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>

这篇关于生成可运行的jar并使用Maven在其中包含库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆