有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Maven插件中打包一个JAR

我有一个maven插件,它有一个需要打包的依赖项(出于某些原因)。因此,我构建我的依赖项,输出一个JAR,并将其安装到目标插件项目中。通过安装,我包括了hash和其他Maven文件,以确保它被正确地拉入。像这样:

<plugin>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>additional-install</id>
            <phase>install</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <createChecksum>true</createChecksum>
                <version>${project.version}</version>
                <groupId>${project.groupId}</groupId>
                <packaging>${project.packaging}</packaging>
                <artifactId>${project.artifactId}</artifactId>
                <file>${project.build.directory}/${project.build.finalName}.jar</file>
                <localRepositoryPath>${project.basedir}/../target-maven-plugin/src/main/resources/libs/repo</localRepositoryPath>
            </configuration>
        </execution>
    </executions>
</plugin>

如您所见,它被放置到目标项目的资源目录中。我不想复制代码,因为这是另一个内部维护项目的分支,不想移动/更新所有代码和测试-我只是重命名了路径

在目标项目中,我创建了一个回购协议,然后使用本地回购协议使用它:

<repositories>
    <repository>
        <id>TargetMavenPluginInternalRepo</id>
        <url>file:${project.basedir}/src/main/resources/libs/repo</url>
    </repository>
</repositories>

然后我将其作为依赖项包括:

<!-- This is a JAR which is located in the libs/ directory. -->
<dependency>
    <groupId>com.company.needed</groupId>
    <artifactId>dependency</artifactId>
    <version>${dependency.version}</version>
</dependency>

项目按预期构建和安装。该插件安装到目标maven插件本地repo中,并自行生成/运行

但是,如果我尝试通过下游项目运行插件,它会失败,因为使用目标Maven插件的下游项目无法找到该依赖项。这有点道理。我可以看到Maven试图解决依赖关系:

Downloading from central: https://repo.maven.apache.org/maven2/com/company/needed/1.1/dependency-1.1.jar
Downloading from TargetMavenPluginInternalRepo: file:${project.basedir}/src/main/resources/libs/repo/com/company/needed/dependency/1.1/dependency-1.1.jar

因此,我继续尝试一些事情,比如将依赖项复制到目标jar中,然后尝试将其添加到类路径:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>${maven.plugin.version}</version>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <artifactItems>
            <artifactItem>
                <groupId>com.company.needed</groupId>
                <artifactId>dependency</artifactId>
                <version>${dependency.version}</version>
                <type>jar</type>
                <overWrite>true</overWrite>
                <outputDirectory>${project.build.directory}/libs</outputDirectory>
            </artifactItem>
        </artifactItems>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>${maven.plugin.version}</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>libs/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>

要做到这一点似乎还有很长的路要走。我的同事建议我把代码复制过来,但我喜欢把它放在外面,这样维护起来更容易,更新也更容易

想法?这可能吗?我不想对外发布这个内部维护的依赖项


共 (0) 个答案