gpt4 book ai didi

java - 在构建时动态添加 ant 插件?

转载 作者:太空宇宙 更新时间:2023-11-04 07:35:27 24 4
gpt4 key购买 nike

我想设置一个 Ant 构建,在构建时拉取其插件。例如,如果我的构建使用 findbugscheckstyle JAR 并从 build.xml 内部调用它们的任务,那么理想情况下我可以:

  1. 运行 ivy-resolveivy-retrieve任务拉findbugscheckstyle JAR 放入,例如 gen/lib/buildtime .
  2. 添加gen/lib/buildtime到 Ant 的类路径
  3. 定义<taskdefs>定义了各种findbugscheckstyle任务
  4. 执行findbugscheckstyle下游目标中的任务

这可能吗?如果是这样,怎么办?我认为这里唯一的阻碍是我不认为你可以动态添加 <taskdefs> ...提前致谢!

最佳答案

最好的方法是使用 cachepath用所需的 jar 填充 ANT 路径的任务。您的任务定义将变为:

<ivy:cachepath pathid="build.path" conf="build"/>
<taskdef uri="antlib:org???" resource="???" classpathref="build.path"/>

ivy 配置可用于控制哪些 jar 应填充路径。

以下示例使用 Sonar自动运行 findbugs 和 checkstyle。它演示了如何使用 ivy 引入以下 ANT 插件:

  • junit
  • 声纳
  • 雅 cocoa

示例

├── build.properties
├── build.xml
├── ivy.xml
└── src
├── main
│   └── java
│   └── org
│   └── demo
│   └── App.java
└── test
└── java
└── org
└── demo
└── AppTest.java

ivy.xml

<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>

<configurations defaultconfmapping="compile->default">
<conf name="compile" description="Required to compile application"/>
<conf name="test" description="Required for test only" extends="compile"/>
<conf name="build" description="Build dependencies"/>
</configurations>

<dependencies>
<!-- compile dependencies -->

<!-- test dependencies -->
<dependency org="junit" name="junit" rev="4.10" conf="test->default"/>

<!-- build dependencies -->
<dependency org="org.codehaus.sonar-plugins" name="sonar-ant-task" rev="2.1" conf="build->default"/>
<dependency org="org.jacoco" name="org.jacoco.ant" rev="0.6.3.201306030806" conf="build->default"/>

<!-- Global exclusions -->
<exclude org="org.apache.ant"/>
</dependencies>

</ivy-module>

build.xml

<project name="demo" default="test" xmlns:ivy="antlib:org.apache.ivy.ant">

<property file="build.properties"/>

<target name="bootstrap" description="Install ivy">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.3.0/ivy-2.3.0.jar"/>
</target>

<target name="resolve" description="Download dependencies and setup classpaths">
<ivy:resolve/>
<ivy:report todir='${reports.dir}/ivy' graph='false' xml='false'/>

<ivy:cachepath pathid="compile.path" conf="compile"/>
<ivy:cachepath pathid="test.path" conf="test"/>
<ivy:cachepath pathid="build.path" conf="build"/>
</target>

<target name="init" depends="resolve" description="Create build directories">
<mkdir dir="${classes.dir}"/>
<mkdir dir="${test.classes.dir}"/>
<mkdir dir="${reports.dir}/junit"/>
</target>

<target name="compile" depends="init" description="Compile source code">
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
</target>

<target name="compile-tests" depends="compile" description="Compile test source code">
<javac srcdir="${test.src.dir}" destdir="${test.classes.dir}" includeantruntime="false" debug="true">
<classpath>
<path refid="test.path"/>
<pathelement path="${classes.dir}"/>
</classpath>
</javac>
</target>

<target name="test" depends="compile-tests" description="Run unit tests and code coverage reporting">
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml" classpathref="build.path"/>

<jacoco:coverage destfile="${build.dir}/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
<junit haltonfailure="yes" fork="true">
<classpath>
<path refid="test.path"/>
<pathelement path="${classes.dir}"/>
<pathelement path="${test.classes.dir}"/>
</classpath>
<formatter type="plain" usefile="false" />
<formatter type="xml"/>
<batchtest fork="yes" todir="${reports.dir}/junit">
<fileset dir="${test.src.dir}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
</target>

<target name="sonar" depends="test" description="Upload metrics to Sonar">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpathref="build.path"/>

<ivy:cachepath pathid="sonar.libraries" conf="compile"/>

<sonar:sonar xmlns:sonar="antlib:org.sonar.ant"/>
</target>

<target name="clean" description="Cleanup build files">
<delete dir="${build.dir}"/>
</target>

<target name="clean-all" depends="clean" description="Additionally purge ivy cache">
<ivy:cleancache/>
</target>

</project>

构建.属性

# Build properties
build.dir=build

src.dir=src/main/java
test.src.dir=src/test/java

classes.dir=${build.dir}/classes
test.classes.dir=${build.dir}/test-classes

reports.dir=${build.dir}/reports

# Sonar properties
sonar.projectKey=org.demo:demo
sonar.projectName=Demo project
sonar.projectVersion=1.0
sonar.projectDescription=This is my demo Sonar project

sonar.host.url=http://localhost:9000

sonar.jdbc.url=jdbc:h2:tcp://localhost:9092/sonar
sonar.jdbc.driverClassName=org.h2.Driver
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

sonar.working.directory=${build.dir}/sonar

sonar.language=java
sonar.sources=${src.dir}
sonar.binaries=${classes.dir}
sonar.tests=${test.src.dir}

sonar.dynamicAnalysis=reuseReports
sonar.surefire.reportsPath=${reports.dir}/junit
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPath=${build.dir}/jacoco.exec

关于java - 在构建时动态添加 ant 插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16947628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com