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

使用Maven生成一个可运行的jar并在其中包含库,maven,jar,dependencies,pom.xml,runnable,Maven,Jar,Dependencies,Pom.xml,Runnable,我正试图用Maven和Eclipse编译一个Java项目,但我尝试了很多在web上看到的解决方案,但它们似乎都不起作用。我只想构建应用程序,创建一个可运行的jar,并包含所需的库。我尝试了maven依赖或maven汇编,但我肯定错过了一些东西,因为每次都失败了 这是我的pom.xml,它可以吗,还是遗漏了什么 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSch

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

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

4.0.0
org.awax
工具箱
0.1.0
工具箱
罐子
http://maven.apache.org
包含有用代码的自定义库
1.7
1.2.17
2.0.5
3.7.4
1.0.19
0.18
2.9
3.2
2.5
2.4.1
2.5.1
log4j
log4j
${log4j.version}
org.jdom
jdom2
${jdom.version}
com.migplayout
混合布局
${miglayout.version}
org.jfree
柱状图
${jfreechart.version}
org.bounce
反弹
${bounce.version}
org.apache.maven.plugins
maven eclipse插件
${eclipse.version}
真的
假的
org.apache.maven.plugins
maven编译器插件
${compiler.version}
${jdk.version}
${jdk.version}
org.apache.maven.plugins
maven依赖插件
${dependency.version}
复制依赖项
包裹
复制依赖项
假如
${project.build.directory}/dependency
org.apache.maven.plugins
maven jar插件
${jar.version}
${project.groupId}.${project.artifactId}.tools.WaveformGenerator
org.apache.maven.plugins
maven汇编插件
${assembly.version}
包裹
单一的
带有依赖项的jar

使用
maven汇编插件
生成具有依赖项的可执行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>

maven汇编插件
org.apache.maven.plugins
2.4.1
使用依赖项生成可执行jar
包裹
单一的
真的
com.coderplus.sample.MainClass
带有依赖项的jar
它不适用于您,因为您已经在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>

org.apache.maven.plugins
maven汇编插件
${assembly.version}
maven汇编插件
org.apache.maven.plugins
使用依赖项生成可执行jar
包裹
单一的
真的
com.coderplus.sample.MainClass
带有依赖项的jar

删除标签
pluginManagemen
并保留其余部分…除此之外,请检查您的build-on命令,而不是在Eclipse中。是的,它工作了!!非常感谢。但是我有两个jar,一个是toolbox-0.1.0,一个是toolbox-0.1.0-jar-with-dependencies,为什么?这就是它应该做的:生成一个包含依赖项的可执行jar和一个普通jar。如果不需要常规的,则将appendAssemblyId设置为false。