如何:Eclipse Maven安装具有依赖关系的构建jar [英] How To: Eclipse Maven install build jar with dependencies

查看:141
本文介绍了如何:Eclipse Maven安装具有依赖关系的构建jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的 pom.xml 如下所示:

 < project xmlns =http://maven.apache.org/POM/ 4.0.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http:// maven .apache.org /行家-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>
<依赖关系>

< 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>
<依赖关系>
<依赖关系>
< 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>
<执行>
< execution>
< id> ant-magic< / id>
< phase> prepare-package< / phase>
< goals>
< goal>运行< / goal>
< / goals>
< configuration>
< tasks>
< property name =compile_classpathrefid =maven.compile.classpath/>
< property name =runtime_classpathrefid =maven.runtime.classpath/>
< property name =test_classpathrefid =maven.test.classpath/>
< property name =plugin_classpathrefid =maven.plugin.classpath/>

< ant antfile =$ {basedir} /clientExport.xmltarget =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 格式和没有解压缩..因为似乎unpacked不正确的工作...



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



我尝试过所有方法:


  1. assembly:assembly / li>
  2. assembly:single

  3. gle 与我的描述符(一个 assemble.xml 文件),但它不工作

  4. maven复制依赖关系插件,但仍然无法使用Eclipse Maven - m2e

我没有解决方案...任何人都可以告诉我一个适当的方法来添加我的依赖项在jar中?我不能相信 maven 是如此复杂,我找不到任何地方的问题的答案。



提前谢谢

解决方案

有几种方法可以这样做。



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

 < build> 
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-shade-plugin< / artifactId>
< version> 1.6< / version>
<执行>
< execution>
< phase> package< / phase>
< goals>
< goal> shade< / goal>
< / goals>
< configuration>
<变压器>
< 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>
<执行>
< 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>运行时< / 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>运行时< / 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中定义依赖关系,例如所以:

 <依赖关系> 
<依赖关系>
< groupId> com.group.id< / groupId>
< artifactId> project-launch1< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< type> jar< / type>
< / dependency>
<依赖关系>
< groupId> com.group.id< / groupId>
< artifactId> project-launch2< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< type> jar< / type>
< / dependency>
<依赖关系>
< groupId> com.group.id< / groupId>
< artifactId> project-lib1< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< type> jar< / type>
< / dependency>
...等等...
< /依赖关系>



3)如果您要交付捆绑可执行的JAR文件启动器,您可能需要添加一个 maven-jar-plugin 配置除了 maven-assembly-plugin

 < dependencies> 
<依赖关系>
< groupId> com.group.id< / groupId>
< artifactId> project-lib1< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< type> jar< / type>
< / dependency>
<依赖关系>
< groupId> com.group.id< / groupId>
< artifactId> project-lib2< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< type> jar< / type>
< / dependency>
<依赖关系>
< groupId> com.group.id< / groupId>
< artifactId> project-lib3< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
< type> jar< / type>
< / dependency>
...等等...
< /依赖关系>

< 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环境变量。


I am using Eclipse Maven (m2e) inside Eclipse and I am running my project like this:

My pom.xml looks like this:

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

After I run the maven install it is working...

Maven run configurations:

The problem is that my generated .jar it doesn't have the dependencies included.... How can I configure pom.xml to include all my dependencies in .jar format and not unpacked.. because it seems that unpacked are not working correct...

To be sure that including all jars is ok.. I downloaded and added each library into jar's /lib folder and the jar is running... so.. my only question is: How can I configure pom.xml in order to add all my dependencies in jar format?

I tried all methods:

  1. assembly:assembly
  2. assembly:single
  3. assembly:single with my descriptor (an assemble.xml file) but it wasn't working
  4. maven copy dependencies plugin but still not working with Eclipse Maven - m2e

I am out of solutions... can anyone tell me a proper way to add my dependencies in jar? I can't believe that maven is so complex and I can't find an answer to my question everywhere..

Thank you in advance

解决方案

There are a couple of ways of doing this.

1) If you want an uber-jar (repacked with all dependencies), look into using and configuring the 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>

This will unpack all dependencies and merge them into one JAR file.


2) If you want to deliver a bundle (zip, tar.gz, etc) with the unpacked JAR files in the bundle (perhaps under lib/) then you need to look into the 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>

Note that this requires an assembly descriptor src/main/assembly/dist.xml and example looks like this:

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

And since you are now assembling dependencies, you have better define the dependency in the pom.xml, like so:

  <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) If you are delivering a bundle with an executable JAR file launcher, you will likely need to add a maven-jar-plugin configuration in addition to the maven-assembly-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>

Note that the "addClasspath" directive adds the project dependencies to the class path. This is needed for JAR launchers, as they explicitly ignore all CLASSPATH environmental variables.

这篇关于如何:Eclipse Maven安装具有依赖关系的构建jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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