如何:Eclipse Maven 安装带有依赖项的构建 jar

2022-09-02 05:34:06

我在Eclipse中使用Eclipse Maven(m2e),我正在运行我的项目,如下所示:

我的样子是这样的: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>ro.project</groupId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>ro.project</name>

    <properties>
        <org.springframework.version>3.1.1.RELEASE</org.springframework.version>
        <org.hibernate.version>4.1.0.Final</org.hibernate.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>ro.project.ProjectServer</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <dependencies>
                    <dependency>
                        <groupId>com.sun</groupId>
                        <artifactId>tools</artifactId>
                        <version>1.7.0_02</version>
                        <scope>system</scope>
                        <systemPath>${java.home}/../lib/tools.jar</systemPath>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>ant-magic</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <property name="compile_classpath" refid="maven.compile.classpath" />
                                <property name="runtime_classpath" refid="maven.runtime.classpath" />
                                <property name="test_classpath" refid="maven.test.classpath" />
                                <property name="plugin_classpath" refid="maven.plugin.classpath" />

                                <ant antfile="${basedir}/clientExport.xml" target="export-all" />
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <artifactId>project-core</artifactId>
    <url>http://www.project.ro</url>
</project>

在我运行maven安装后,它正在工作...

Maven 运行配置:

maven install deploy jar

问题是我生成的它没有包含依赖项....如何配置以格式包含所有依赖项而不解压缩。因为似乎解压缩工作不正确....jarpom.xml.jar

确保包括所有罐子是可以的。我下载并将每个库添加到 的文件夹中,并且jar正在运行...所以。。我唯一的问题是:如何配置以格式添加所有依赖项?jar/libpom.xmljar

我尝试了所有方法:

  1. assembly:assembly
  2. assembly:single
  3. assembly:single使用我的描述符(文件),但它不起作用assemble.xml
  4. maven copy dependencies插件,但仍然不能与Eclipse Maven一起使用 -m2e

我没有解决方案...任何人都可以告诉我一种在jar中添加依赖项的正确方法吗?我不敢相信这太复杂了,我无法到处找到我问题的答案。maven

提前感谢您


答案 1

有几种方法可以做到这一点。

1)如果你想要一个uber-jar(重新打包所有依赖项),请考虑使用和配置maven-shade-plugin

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>com.group.id.Launcher1</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

这将解压缩所有依赖项,并将它们合并到一个 JAR 文件中。


2)如果你想提供一个捆绑包(zip,tar.gz等),其中包含捆绑包中未打包的JAR文件(可能在lib/下),那么你需要查看maven-assembly-plugin

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.3</version>
        <executions>
          <execution>
            <id>create-distro</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>src/main/assembly/dist.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

请注意,这需要一个程序集描述符,示例如下所示:src/main/assembly/dist.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0">
  <id>distribution</id>
  <formats>
    <format>zip</format>
  </formats>

  <dependencySets>
    <dependencySet>
      <useProjectArtifact>false</useProjectArtifact>
      <useTransitiveDependencies>false</useTransitiveDependencies>
      <unpack>false</unpack>
      <scope>runtime</scope>
      <fileMode>0755</fileMode>
      <directoryMode>0755</directoryMode>
      <outputDirectory>bin</outputDirectory>

      <includes>
        <include>com.group.id:project-launch1</include>
        <include>com.group.id:project-launch2</include>
      </includes>

    </dependencySet>
    <dependencySet>
      <useProjectArtifact>false</useProjectArtifact>
      <useTransitiveDependencies>true</useTransitiveDependencies>
      <unpack>false</unpack>
      <scope>runtime</scope>
      <fileMode>0644</fileMode>
      <directoryMode>0755</directoryMode>
      <outputDirectory>lib</outputDirectory>

      <includes>
        <include>com.group.id:project-lib1</include>
        <include>com.group.id:project-lib2</include>
        <include>com.group.id:project-lib3</include>
        <include>com.group.id:project-lib4</include>
      </includes>

    </dependencySet>
  </dependencySets>
</assembly>

由于您现在正在组装依赖项,因此最好在 pom.xml中定义依赖项,如下所示:

  <dependencies>
    <dependency>
      <groupId>com.group.id</groupId>
      <artifactId>project-launch1</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>com.group.id</groupId>
      <artifactId>project-launch2</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>com.group.id</groupId>
      <artifactId>project-lib1</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <type>jar</type>
    </dependency>
    ... and so on ...
  </dependencies>


3) 如果您要交付带有可执行 JAR 文件启动器的捆绑包,则除了 maven-assembly-插件之外,您可能还需要添加 maven-jar-plugin 配置

  <dependencies>
    <dependency>
      <groupId>com.group.id</groupId>
      <artifactId>project-lib1</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>com.group.id</groupId>
      <artifactId>project-lib2</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>com.group.id</groupId>
      <artifactId>project-lib3</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <type>jar</type>
    </dependency>
    ... and so on ...
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
            <compress>true</compress>
            <manifest>
              <mainClass>com.group.id.Launcher1</mainClass>
              <addClasspath>true</addClasspath>
              <classpathPrefix>../lib/</classpathPrefix>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

请注意,“addClasspath”指令将项目依赖项添加到类路径中。这对于 JAR 启动器是必需的,因为它们显式忽略所有 CLASSPATH 环境变量。


答案 2

推荐