微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 antrun maven 插件从插件类路径中执行 jar

我有一个构建步骤,它使用 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?