简体   繁体   English

使用Maven将另一个项目的jar添加为资源

[英]Adding another project's jar as a resource using Maven

Within my project I have a sub-project auto-updater. 在我的项目中,我有一个子项目自动更新程序。 Basically a jar file that is extracted and run when an update is available. 基本上是在更新可用时提取并运行的jar文件。

Is it possible to compile the sub-project, then place the outputted jar as a generated-resource so that the updater.jar is included in the final jar such as: 是否可以编译子项目,然后将输出的jar作为generated-resource放置,以便updater.jar包含在最终的jar中,例如:

Project-1.0.jar
 |-updater.jar
    |-Main.class
    |-B.class

Thanks in advance for any help(I'm new to Maven) 在此先感谢您的帮助(我是Maven的新手)

This task is calling for maven-assembly-plgin or maven-dependency-plugin 此任务调用maven-assembly-plginmaven-dependency-plugin

(I expect that updater is also maven project) this shoudl be proper configuration for maven-dependency-plugin [I did not test this, you might also need to put updater to project depndencies] (我希望更新程序也是maven项目)这个shoudl是maven-dependency-plugin的正确配置[我没有测试这个,你可能还需要将updater放到项目depndencies]

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>copy</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>company.com.project</groupId>
                  <artifactId>Updater</artifactId>
                  <version>0.0.1-SNAPSHOT</version>
                  <type>jar</type>
                  <outputDirectory>${project.build.outputDirectory}/classes</outputDirectory>
                  <destFileName>updater.jar</destFileName>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

It would be nice if your little jar is also built with Maven. 如果你的小罐子也是用Maven构建的话会很好。 Then let's say your little jar POM's contains this: 那么让我们说你的小罐子POM包含这个:

<groupId>company.com.project</groupId>
<artifactId>Updater</artifactId>
<version>0.0.1-SNAPSHOT</version>

Then your Project-1.0 should use this dependency: 然后你的Project-1.0应该使用这个依赖:

<dependency>
    <groupId>company.com.project</groupId>
    <artifactId>Updater</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

Add the sub project to your parent project as follows 将子项目添加到父项目,如下所示

<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>1.0.0</modelVersion>
    <groupId>Project-1.0</groupId>
    <artifactId>myproject</artifactId>
    <packaging>pom</packaging>

    <version>1.0-SNAPSHOT</version>

    <name>Project-1.0</name>

    <modules>
        <module>../updater</module>
    </modules>

    ...
</project>

Then in you updater project pom file make the following changes 然后在您的updater项目pom文件中进行以下更改

<?xml version="1.0"?>
<project>
  <parent>
    <artifactId>myproject</artifactId>
    <groupId>Project-1.0</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>

  <groupId>Project-1.0</groupId>
  <artifactId>updater</artifactId>
  <version>1.0-SNAPSHOT</version>

  ...

</project>

When you compile your parent project, the child project will automatically get compiled. 编译父项目时,子项目将自动编译。

Then to add the sub project jar in your jar, add the following plugin in parent pom.xml 然后在jar中添加子项目jar,在父pom.xml中添加以下插件

 <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <phase>process-resources</phase>
        <configuration>
          <tasks>
            <copy todir="${project.build.directory}/lib">
              <fileset dir="${location of updater1.0.jar}"/>
            </copy>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM