I am trying to integrate Sonar into my ant build script. I am able to make it work if I edit the build file of respective child projects by adding
<!-- Define the SonarQube project properties. removing the rest of properties for representing the problem-->
<property name="sonar.sources" value="src" />
<!-- Define SonarScanner for Ant Target -->
<target name="sonar" xmlns:sonar="antlib:org.sonar.ant">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<!-- Update the following line, or put the "sonarqube-ant-task-*.jar" file in your "$HOME/.ant/lib" folder
<classpath path="path/to/sonar/ant/task/lib/sonarqube-ant-task-*.jar" />-->
</taskdef>
<!-- Execute SonarScanner for Ant Analysis -->
<sonar:sonar />
</target>
as defined in https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-ant/
Now I want to move this config a level up from the sub project level to the root level so that I need to edit just one build file.
I was not able to move the above block as such since root project does not have a src folder and ant complained about that. So I tried to have a subant task for this
<subant target ="sonar" xmlns:sonar="antlib:org.sonar.ant">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<!-- Update the following line, or put the "sonarqube-ant-task-*.jar" file in your "$HOME/.ant/lib" folder
<classpath path="path/to/sonar/ant/task/lib/sonarqube-ant-task-*.jar" />-->
</taskdef>
<!-- Execute SonarScanner for Ant Analysis -->
<sonar:sonar />
</subant>
But for this I get the following error:
subant doesn't support the nested "taskdef" element.
Is there any workaround for this?
I've made a test building my project solution twice in parallel to see what happens. Turns out one MSBuild node waits the other to finish before trying to build my solution and then says it's up-to-date. Ex.:
<!-- ProjectA.proj -->
<Project>
<ItemGroup>
<Project Include="MyProject.sln" />
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="#(Project)" Targets="Build" BuildInParallel="true" />
</Target>
</Project>
<!-- Test.proj -->
<Project>
<ItemGroup>
<Project Include="ProjectA.proj" />
<Project Include="ProjectA.proj" />
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="#(Project)" Targets="Build" BuildInParallel="true" />
</Target>
</Project>
<!-- When I check the build log it says
1> Project is building MyProject.sln (2) ...
1> Project is building MyProject.sln (2:2) ...
2> Building with tools...
2> ...
2:2> Building with tools...
2> ...
2> ...
2> ...
2> Done building
2:2> Target ... skipped because it was previously built successfully
2:2> Done building
-->
This is a desired behaviour, but when I try to rely on this to make my build workflow oh the build server, my projects are built twice simultaneously, giving me lots of errors.
I do not understand the difference and don't know what to do to find out the problem.
I have a project with a dependency to some other Java classes from another project.
In eclipse the two projects are running without any errors.
Now I want to build the project with the dependency with Jenkins and Ant.
I get the dependency from Ivy with this config:
<publications>
<artifact name="${version.name}" type="war" ext="war"/>
</publications>
<dependencies>
<dependency org="de.company.sh" name="rest-db" rev="latest.integration"/>
</dependencies>
The retrieve in the build.xml:
<!-- retrieve dependencies from ivy -->
<target name="retrieve" depends="prepare, prepare.productive, prepare.beta">
<!-- Fetch parameters from properties -->
<property file="build.properties" />
<!-- Resolve module -->
<ivy:resolve file="ivy.xml" />
<!-- Retrieve dependencies -->
<ivy:retrieve pattern="${build}/deps/[organization]/[module]/[artifact]-[revision].[ext]" />
</target>
The console log from Jenkins:
[ivy:resolve] Apr 24, 2013 1:42:06 PM org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
[ivy:resolve] INFO: Basic authentication scheme selected
[ivy:resolve] downloading http://localhost:7080/nexus/content/repositories/snapshots/de/company/sh/rest-db/1.0.3269/wars/rest-db-1.0.3269.war ...
Up to this point everything is ok
But now I have the problem that I canĀ“t include the class files from the war file.
Folder structure from the war file to the relevant class files:
rest-db-1.0.3269.war\WEB-INF\classes\de\company\sh...
Here are my attempts, with no success :-(
<!-- create war file from java classes -->
<target name="war" depends="compile">
<!-- Move rest-db where it is needed
<war destfile="${build}/lib/rest-db.war" webxml="WEB-INF/web.xml">
<zipfileset dir="${build}/classes" includes="**/*.class"/>
</war>
-->
<!-- -->
<zip destfile="${build}/deps/de.company.sh/rest-db" basedir="${build}" update="true" />
<!-- Copy all necessary Spring libraries -->
<copy todir="${build}/lib">
<!-- All files directly located in the lib/ directory -->
<fileset dir="${lib}">
<include name="*.jar" />
</fileset>
</copy>
...
<!-- Move rest-db where it is needed
<copy todir="${build}/classes">
<fileset dir="${build}/deps/de.company.sh/rest-db">
<include name="*.*" />
</fileset>
</copy>
-->
I hope you have some brilliant suggestions for me :-)
Sounds like you're trying to declare a dependency on artifacts inside an artifact already downloaded using ivy?
Perhaps the packager resolver is where you need to look. Powerful although a bit complicated to setup.
For examples see:
ivy dependency on external JAR
gradle - how to declare a dependency of a jar in a jar
hOW can i add external jars in my build.xml.
i am getting compilation error while running my build.xml.some jars are missing.how can i add them in my build.xml.
my build.xml looks like this
<project name="HUDSONSTATUSWS" default="dist" basedir=".">
<description>
Web Services build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<property name="webcontent" location="WebContent"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="war" depends="compile"
description="generate the distribution war" >
<!-- Create the war distribution directory -->
<mkdir dir="${dist}/war"/>
<!-- Follow standard WAR structure -->
<copydir dest="${dist}/war/build/WEB-INF/" src="${webcontent}/WEB-INF/" />
<copydir dest="${dist}/war/build/WEB-INF/classes/" src="${build}" />
<jar jarfile="${dist}/war/HelloWorld-${DSTAMP}.war" basedir="${dist}/war/build/"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
You just need to specify the classpath for javac, using the classpath or classpathref attribute, or a nested classpath element.
See http://ant.apache.org/manual/Tasks/javac.html for details. The ant documentation is very well written, exhaustive, and full of examples. It's there to be read.
I would like to do something like this
<target name="clean" description="clean">
<if>
<available file="${build}" type="dir" />
<then>
<delete dir="${build}" />
</then>
</if>
</target>
As per suggestions found on stackoverflow.com, i downloaded ant-contrib-1.0b3.jar and put it into my path
Additionally, under Ant Build configuration, in classpath, i have
When running my ANT script, i still get
BUILD FAILED
C:\Repositories\blah\build.xml:60: Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
What am i missing? Please advise
Alternately, once can include the following line in your ANT script
<taskdef resource="net/sf/antcontrib/antlib.xml" />
As long as ant-contribs is in your path, nothing else is needed
This solution is a bit cleaner, as one gains access to ALL the tags, not just the ones manually specified
Same issue resolved in that way.
I put at the start of the buid XML file the following lines:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="your/path/to/ant-contrib-${version}.jar" />
</classpath>
</taskdef>
The standard ant way would be something like =
<target name="check">
<condition property="delbuild">
<available file="${build}" type="dir"/>
</condition>
</target>
<target name="delbuild" depends="check" if="delbuild">
<delete dir="${build}"/>
<!-- .. -->
</target>
A snippet with the Ant Plugin Flaka, a recent alternative to antcontrib. Installation in Eclipse as usual via Preferences | Ant | Runtime | Global Entries | ant-flaka-1.02.-1.02.jar =
<project xmlns:fl="antlib:it.haefelinger.flaka">
<!-- some standalone if construct -->
<fl:when test=" '${build}'.isdir ">
<delete dir="${build}"/>
</fl:when>
<!-- some if/then/else construct -->
<fl:choose>
<!-- if -->
<when test=" '${buildtype}' eq 'prod' ">
<!-- then -->
<echo>..starting ProductionBuild</echo>
</when>
<when test=" '${buildtype}' eq 'test' ">
<!-- then -->
<echo>..starting TestBuild</echo>
</when>
<!-- else -->
<otherwise>
<fl:unless test="has.property.dummybuild">
<fail message="No valid buildtype !, found => '${buildtype}'"/>
</fl:unless>
<echo>.. is DummyBuild</echo>
</otherwise>
</fl:choose>
</project>
output with ant -f build.xml -Dbuildtype=prod or
ant -f build.xml -Dbuildtype=prod -Ddummybuild=whatever
[echo] ..starting ProductionBuild
output with typo => ant - build.xml -Dbuildtype=testt
BUILD FAILED
/home/rosebud/workspace/AntTest/build.xml:21: No valid buildtype !, found => 'testt'
output with ant -f build.xml -Ddummybuild=whatever
[echo] .. is DummyBuild
I got the same issue. I resolved in the following way.
Sometimes this might be compatibility problem.
Right click on build.xml.
Go to "Run As" --> 2 Ant... Select Classpath tab check Ant Home version (Sometimes eclipse selects default ant version).
If the version listed is different, then change Ant Home Classpath
to C:\XXXX\ant\X.X.X.
Finally click on the User Entries --> Add External JARS..--> add
ant-contrib.x.x.jar form C:\XXXX\ant\X.X.X\ant-contrib\ directory.
On the tasks tab for your Ant Runtime do you see the 'if' task?
export ANT_HOME=/path/to/ant/apache-ant-1.8.2
I couldn't get it to work until I had ANT_HOME set properly. Java kept picking another ant installed on the box.