Ant Target to Expand Archives of Dependency - maven

I have an Ant/Ivy build process that I am trying to improve. I need to resolve dependencies and then extract them to a particular folder.
The dependencies resolve to a $(build_root)/dependency/downloads/[configuration]/[artifactId]/[version]/ location and this works fine. I end up with a .pom and a .tar.bz2 file in that location.
What I would like to do is have a bit more control over the extraction of the dependency's .tar.bz2 to a directory. We're basically trying to prevent conflicts for a dependency's lib and inc by controlling how and where the extraction of the .tar.bz2 output occurs.
We currently do this by resolving all the dependencies, and then having a target to expand the archives blindly.
Our resolve step looks like:
<target name="resolve">
<ivy:retrieve pattern="${dependency.dir}/[conf]/[artifact]/[artifact]-[revision].[ext]" conf="*" />
<condition property="archive.dir.present">
<resourceexists>
<file file="${dependency.dir}"/>
</resourceexists>
</condition>
<antcall if:set="archive.dir.present" target="expand-archives"/>
</target>
Note the separate call to the "expand-archives" target, which looks like:
<target name="expand-archives" description="Expand your Dependency Archives!">
<for param="file">
<path>
<fileset dir="${dependency.dir}" includes="**/*.tar.bz2"/>
</path>
<sequential>
<bunzip2 src="#{file}" dest="${dependency.dir}" />
</sequential>
</for>
<!-- Follow the bunzip2 by the tar command to extract the tarball -->
<for param="file">
<path>
<fileset dir="${dependency.dir}" includes="**/*.tar"/>
</path>
<sequential>
<echo message="Processing: #{file}"/>
<exec executable="tar" failonerror="true">
<arg value="-C"/>
<arg value="${dependency.dir}"/>
<arg value="-xvf"/>
<arg value="#{file}"/>
</exec>
</sequential>
</for>
</target>
What I would love to have is the dest="${dependency.dir} have a dependency's artifactID appended to it.
Is there any way to get the [artifact] information from the retrieve and pass that into the expand-archives target?
Thank you

If you have dependencies on files located within archives, perhaps you should investigate the packager resolver in ivy.
This resolver is designed to download + extract archives and then use an ANT snippet to decide which files within you want to depend on. At first glance complicated, but once understood very powerful.
The following example shows how to depend on a jar, contained inside a downloadable ".tar.gz" archive:
ivy dependency on external JAR

Related

Maven regular expression to match file is not working

I have two files test-200-12-30-2990 and test-project-200-12-30-2990 I am unziping it to corresponding folders. Second task is not working. I want to select the file 'test-200-12-30-2990'
Below is the ant build.xml. I am using maven ant plugin inside pom
<mkdir dir="/testdir"/>
<unzip dest="src/main/resources/testdir">
<fileset dir="src/main/resources">
<include name="**/test-project*.zip"/>
</fileset>
</unzip>
</target>
<mkdir dir="/test-projectdir"/>
<unzip dest="src/main/resources/test-projectdir">
<fileset dir="src/main/resources">
<include name="**/test[1-9].zip"/>
</fileset>
</unzip>
</target>
I'm not quite sure how inclusion might work in Maven, but maybe we could solve this problem with a simple expression that you already have, something maybe similar to:
test-[0-9-]+
Then, the include might look like:
test-[0-9-]+\.zip
**/test-[0-9-]+\.zip
If escaping . might be unnecessary, then we can just use:
**/test-[0-9-]+.zip
DEMO
Reference
Include xml files in maven project

How to convert an ant snippet to Gradle

Hi I am completely new to Gradle. Can anyone please help me as to how I can convert this ant task into Gradle. I am trying to see if I can learn by example.
<property file="build.properties"/>
<path id="classpath">
<fileset dir="${ear.dir}/EarContent/APP-INF">
<include name="**/*.jar" />
</fileset>
</path>
<target name="clean">
<delete file="${dist.dir}/*.jar" failonerror="false"/>
<delete failonerror="false">
<fileset dir="${build.dir}">
<include name="**/*.class" />
</fileset>
</delete>
</target>
<target name="init">
<mkdir dir="${dist.dir}"/>
<mkdir dir="${build.dir}/classes"/>
</target>
<target name="jar" depends="clean, init">
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" debug="on">
<classpath refid="classpath"/>
</javac>
<jar destfile="${dist.dir}/phIntegration.jar">
<fileset dir="${build.dir}/classes" />
</jar>
</target>
I'm not convinced that trying move ANT build 1-1 to Gradle is a good learning exercise, although Gradle provides good integration with ANT.
Generally speaking, the build.gradle file for your project shall like somewhat like that:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'CLASSPATH_DIR', include: ['**/*.jar'])
//if you want you can use JARS from your filesystem as a classpath
//but you probably should use Ivy or Maven dependency management systems
}
sourceSets {
main {
java 'src/main/java'
//tweak the location of your source code, by default Gradle looks for sources in `src/main/java`
}
}
Once you invoke gradle clean build it will compile your sources and create a jar file containing the sources in build/libs directory. You can change the JARS name by adding the rootProject.name='your project name' in the settings.gradle directory.
The snippet uses Java plugin and the default behaviour of Gradle. If you want you can tweak its behaviour by using Delete Task or Jar Task.
If you define properly inputs/outputs of a task the init task won't be necessary since Gradle will take care of directories recreation if they're not existent. (Default tasks do that, if you start writing your own tasks you most probably will have to take care of that by your own)

How to use skipTests option when using mvn-ant-tasks in ant

I am using mvn-ant-task in a build file to clean and install the Maven projects.
Below is the configuration I did
<project name="maven-project"
default="default"
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
and in the target, I have added something like this
<target name="CleanInstall">
<echo message="Building the project using maven and skipping tests if any in ${RepoFolder}" />
<artifact:mvn pom="${GIT_REPO_HOME}\${RepoFolder}\pom.xml">
<arg value="clean:clean" />
</artifact:mvn>
<artifact:mvn pom="${GIT_REPO_HOME}\${RepoFolder}\pom.xml">
<arg value="install:install -DskipTests=true" />
</artifact:mvn>
</target>
When I run this build, clean is processing properly, but I am unable to send the arguments skipTests in the install plugin.
Is there any other way to execute the Maven commands from Ant? I also tried
<exec command="mvn install -DskipTests=true" dir=""${GIT_REPO_HOME}\${RepoFolder}\pom.xml"/>
Its failing with error given below
C:\Users\sharath\Desktop\devsetup\build.xml:102: Execute failed:
java.io.IOException: Cannot run program "mvn" (in directory
"D:\sbhaskara\GIT\connectmodel"): CreateProcess error=2, The system
cannot find the file specified
You are not building the argument for the task correctly, it should be:
<target name="CleanInstall">
<echo message="Building the project using maven and skipping tests if any in ${RepoFolder}" />
<artifact:mvn pom="${GIT_REPO_HOME}\${RepoFolder}\pom.xml" >
<arg value="-Dmaven.test.skip=true" />
<arg value="clean" />
<arg value="install" />
</artifact:mvn>
</target>
i.e. each argument must be in its own <arg> tag. Note that I collapsed all the calls to Maven inside a single task.

Using jarbundler ant task to add dependencies to the app

I have a Netbeans Java project. When I build my project it create a directory dist and dist/lib. It stores the Jar of the file in dist and other jar files on which the main jar file depends, in the lib directory.
Now I want to create a release for OSX. For that I am using the jarbundler ant task like this
<target name="mac">
<mkdir dir="release"/>
<taskdef name="jarbundler"
classname="net.sourceforge.jarbundler.JarBundler" />
<jarbundler dir="release"
name="MyApp"
mainClass="controller.MyApp"
jar="dist/MyApp.jar" />
</target>
This creates the app with the jar, but how do I add the dependent libraries to the app.
This is what is needed
The jar attribute should be replaced with jarfileset like this.
<target name="mac">
<mkdir dir="release"/>
<taskdef name="jarbundler"
classname="net.sourceforge.jarbundler.JarBundler" />
<jarbundler dir="release"
name="MyApp"
mainClass="controller.MyApp">
<jarfileset dir="dist">
<include name="**/*.jar" />
</jarfileset>
</jarbundler>
</target>

How do I create a ZIP file of my Cruise Control builds?

I use CruiseControl.NET to automatically build my .NET 3.5 web applications, which works a treat. However, is there any way to automatically create a ZIP file of these builds, and put the ZIP's into a separate directory?
I have seen this is possible using NAnt but cannot find an example of how to get this working.
Can anyone offer help/examples?
I've just added such a Nant task to our CC machine.
See http://nant.sourceforge.net/release/latest/help/tasks/zip.html
Note when initially viewing the zip archive, it may appear as if all the files are at the same level, i.e no folders, but actually they folders are preserved.
Notice how you can exclude file types or folders.
You could take the approach of only including the file types you want and excluding the rest.
First define properties for where the source files are allcode.dir and the name and location of the zip file sourcebackup.zip
Now here is the nant task
<zip zipfile="${sourcebackup.zip}" includeemptydirs="true" verbose="true">
<fileset basedir="${allcode.dir}">
<include name="**/*" />
<exclude name="**/_resharper*/**" />
<exclude name="**/build/**" />
<exclude name="**/obj/**" />
<exclude name="**/bin/**" />
<exclude name="**/*.dll" />
<exclude name="**/*.scc" />
<exclude name="**/*.log" />
<exclude name="**/*.vssscc" />
<exclude name="**/*.suo" />
<exclude name="**/*.user" />
<exclude name="**/*.pdb" />
<exclude name="**/*.cache" />
<exclude name="**/*.vspscc" />
<exclude name="**/*.msi" />
<exclude name="**/*.irs" />
<exclude name="**/*.exe" />
</fileset>
<echo message="########## Zipped##########" />
Call this from your cc build like any other nant task.
We find it best if each CC project calls a single task if possible, then you only have to change the nant script, and you can run the nant script on your local machine.
Eg in the project block, we have the single target "build", which as part of its work calls ZipSource
<targetList>
<target>Build</target>
</targetList>
We use the above for a BizTalk project.
Enjoy.
If you're using Nant, then doesn't the Zip task work for you?
We are zipping the sources of a CruiseControl.NET project
but we are using ant
<target name="zipProject">
<mkdir dir="output"/>
<zip destfile="output\sources.zip" basedir="C:\project\src" />
</target>
i don't know about nant but i would expect it to be similar
#David: The NAnt Zip task is what I'm after, yes, but I'm asking how to integrate it as part of an automatic CruiseControl.NET build. If you take a look at the NAnt documentation for the cruise control config it doesn't make it clear if I can run an NAnt task from inside the <tasks> XML node in my CruiseControl config - it only says that it can be part of a <schedule>.
I have found a few examples of setting up your CruiseControl config and a few examples of NAnt tasks but nothing that integrates the two: specifically, zipping up a CruiseControl build.
If anyone has some sample XML of their CruiseControl config, hooking up to an NAnt zip task, post samples here.
Cheers.

Resources