使用 antrun maven 插件从插件类路径中执行 jar

时间:2021-06-01 08:44:01

标签: maven maven-antrun-plugin

我有一个构建步骤,它使用 antrun 插件来执行代码生成器。代码生成器可作为 maven 插件使用,仅在构建步骤中需要。

我们将代码生成器依赖项作为应用程序的依赖项,并希望将其从打包中排除。我发现代码生成器不是应用程序的依赖项,因此它应该是插件的依赖项。

这曾经有效。

    <dependencies>
        <dependency>
            <groupId>com.myCompany</groupId>
            <artifactId>codeGenerator</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>codeGeneration</id>
                        <phase>initialize</phase>
                        <configuration>
                            <target>
                                <echo message="jar location:  ${com.myCompany:codeGenerator:jar}"/>
                                <java jar="${com.myCompany:codeGenerator:jar}"
                                      fork="true"
                                      failonerror="true">
                                    <arg value="--input"/>
                                    <arg value=..."/>
                                </java>
                </executions>
            </plugin>
        </plugins>
    </build>

来自 maven 依赖项的 jar 被执行。 jar 文件解析成功。

<块引用>

[警告] [echo] jar 位置:/home/.../.m2/repository/com/mycompany/codegenerator/1.0/codegenerator.jar

我尝试将依赖项放入插件定义中并使用 maven.plugin.classpath 而不是 maven.dependency.classpath

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>codeGeneration</id>
                        <phase>initialize</phase>
                        <configuration>
                            <target>
                                <property name="plugin_classpath" refid="maven.plugin.classpath"/>
                                <echo message="plugin classpath:  ${plugin_classpath}"/>
                                <echo message="jar location:  ${com.mycompany:codeGenerator:jar}"/>
                                <java jar="${com.myCompany:codeGenerator:jar}"
                                      fork="true"
                                      failonerror="true"
                                      classpathref="maven.plugin.classpath">
                                    <arg value="--input"/>
                                    <arg value=".../>
                                </java>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.myCompany</groupId>
                        <artifactId>codeGenerator</artifactId>
                        <version></version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

类路径看起来不错,生成器已包含在内,但是一旦我从应用程序的依赖项中删除代码生成器,<java jar="${com.myCompany:codeGenerator:jar} 中的 jar 引用就不再起作用。

<块引用>

[警告] [echo] jar 位置:${com.myCompany:codeGenerator:jar} [INFO] [java] 错误:无法访问 jarfile /home/... /${com.myCompany:codeGenerator:jar}

为什么jar的分辨率停止工作?它是否以某种方式依赖于依赖类路径?有什么办法可以使它与插件类路径一起使用吗?

P.S.:我更喜欢在单独的 maven 模块中生成代码,但我想这只会移动问题而不是解决它。

解决方法:定义一个属性来手动构建路径

    <codegenerator-jar>
        <!-- Workaround: Normal jar resolution with ${groupId:artifactId:jar} only works if the dependency is in the dependency classpath. The code generator is only on the plugin classpath -->
        ${settings.localRepository}/com/mycompany/codegenerator/${sdk-version}/codegenerator-${sdk-version}.jar
    </codegenerator-jar>

0 个答案:

没有答案