【问题标题】:How To: Eclipse Maven install build jar with dependencies如何:Eclipse Maven 安装带有依赖项的构建 jar
【发布时间】:2015-06-05 19:47:37
【问题描述】:

我在 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 运行配置:

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

为了确保包括所有 jar 都可以。我下载每个库并将其添加到 jar/lib 文件夹中,并且 jar 正在运行...所以.. 我唯一的问题是:我该如何配置pom.xml 以便以jar 格式添加我的所有依赖项?

我尝试了所有方法:

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

我没有解决方案...谁能告诉我在 jar 中添加依赖项的正确方法?我不敢相信maven 如此复杂,我到处都找不到我的问题的答案..

提前谢谢你

【问题讨论】:

  • 除非你有一个 jar 加载类加载器,否则你不能像你想要做的那样嵌套 jars。
  • 我要像 java -jar myjar 一样运行它

标签: java maven dependencies maven-3 m2eclipse


【解决方案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 环境变量。

【讨论】:

  • 一般的 jar 可以包含 jar,但类加载器不会从 jar 中加载 jar,因此它要么是 uber-jar,要么是具有适当相对位置的文件“包”。
  • 谢谢你的回答......我想提供一个jar,我可以用java -jar myProject.jar执行......你在这里告诉我的不是添加我的包......怎么能我这样做?非常感谢...我还想在罐子里设置我的classpath...
  • 现在...它正在将我的libs 添加到zip 文件夹(或 jar)中,但我的项目不存在.. 结果类似于:project-1.0.SNAPSHOT(inside is只有一个 lib 文件夹) META-INF
  • @Alex。在程序集描述符中将 &lt;useProjectArtifact&gt; 设置为 true 以打包项目工件。
  • @Alex 每个工具都有其缺陷。 Maven 有很好的依赖管理,所以让它使用它的依赖管理来管理 zip 文件的依赖元素(可运行的 jar,其他库)是有意义的。但是,这确实意味着您可以获得更多“小” maven 项目,而不是一个大项目(就像您可能对 ant 所做的那样)。您的初始请求完全按照您的要求“包含 jar 的单个 jar”无论如何都不起作用,因为 Java 的类加载器无法从 Jar 文件中加载 Jar 文件,它只能从 Jar 中加载类文件文件。
猜你喜欢
  • 1970-01-01
  • 2018-01-20
  • 2013-07-19
  • 2020-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多