the structure of the example project is:
.
|-- ./build
| `-- ./build/TestAntLoadFile.class
|-- ./build.xml
|-- ./dist
| |-- ./dist/icpFinder.jar
| `-- ./dist/icp-finder.properties
|-- ./icp-finder_bak.properties
`-- ./src
`-- ./src/TestAntLoadFile.java
and the code getting the properties file is:
public class TestAntLoadFile {
private static final String CUSTOMER_CONFIG_FILE_NAME
= "icp-finder.properties";
public static void main(String[] args) {
InputStream custumerConfigIn = TestAntLoadFile.class.
getClassLoader().getResourceAsStream(CUSTOMER_CONFIG_FILE_NAME);
System.out.println("custumerConfigIn: " + custumerConfigIn);
}
}
and build.xml core contend is :
<path id="run.classpath">
<fileset dir = "${dist.dir}" >
<include name="**/*.jar"/>
<include name="**/*.properties"/>
<include name="./icp-finder.properties"/>
</fileset>
</path>
<target name="run" depends="jar">
<java fork="true" classname="TestAntLoadFile">
<classpath>
<path refid="run.classpath"/>
</classpath>
</java>
</target>
the project run well in eclipse, Has anybody got any suggestions?
Rather than including the properties file itself in the classpath, you need to include the directory it resides in, something like this for example:
<path id="run.classpath">
<fileset dir="${dist.dir}" >
<include name="**/*.jar"/>
</fileset>
<dirset dir="${dist.dir}" />
<pathelement path="${dist.dir}" />
</path>
Related
I'm trying to migrate from ant to gradle. First phase of this is to move all dependecies to gradle.build and still build war via ant.
In ant building task looks like that:
<fileset id="project-libraries" dir="${project.libs.path}">
<include name="*jar"/>
</fileset>
<path id="master-classpath">
<fileset refid="project-libraries"/>
<fileset refid="tomcat"/>
<fileset refid="hibernate-tools"/>
<fileset refid="findbug"/>
<pathelement path="${build.dir}"/>
</path>
<target name="build" description="Build the application">
<javac destdir="${build.dir}" target="${javac.version}" source="${javac.version}" nowarn="true" deprecation="false" optimize="false" failonerror="true" encoding="utf-8" debug="on">
<src refid="src.dir.set"/>
<classpath refid="master-classpath${master-classpath-version}"/>
<compilerarg value="-Xlint:-unchecked"/>
</javac>
</target>
In Gradle I'm importing build.xml with this code:
ant.importBuild('build.xml') { antTargetName ->
'ant_' + antTargetName
}
The problem is that ant task (./gradlew ant_build) doesn't have dependencies from Gradle (dependencies { ... }). How can I put them into classpath (without modifying ant build)?
You can do the following to add the dependencies to the project's AntBuilder instance:
task antClasspathSetter {
doLast {
def antClassLoader = org.apache.tools.ant.Project.class.classLoader
configurations.compile.each { File f ->
antClassLoader.addURL(f.toURI().toURL())
}
}
}
ant_build.dependsOn antClasspathSetter
However, this is a 'hacky' solution.
Using taskdef is a better solution, if the ant build script can be moved to a separate ant task file. In that case, you can do the following:
ant.taskdef(name: 'myAntTask',
classname: 'my.ant.Task',
classpath: configurations.compile.asPath)
I used a copy task to put all of my gradle dependencies into a {libs} folder that I declared on my ant master-classpath.
//add property
<property name="lib.dir" value="${basedir}/lib" /></pre>
//tell ANT to put all jars in folder on classpath
<path id="master-classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
....
</path>
// copy task goes in your build.gradle file
task copyGradleDependenciesInAntFolder(type: Copy) {
from configurations.compile
into 'lib'
}
// make sure to run it before your {ant_build} target
{ant_build}.dependsOn copyGradleDependenciesInAntFolder
I need to process a directory if it has at least one modified file in it. I wrote a block that reduces a fileset to a unique list of the directories that contain those files, but I think this would be easier if there was a way to do this without the script.
Is there a way?
Tricky to do this with core ANT.
Here's an example using an embedded groovy script:
<project name="demo" default="process-modified-dirs">
<path id="build.path">
<pathelement location="/path/to/groovy-all/jar/groovy-all-2.1.1.jar"/>
</path>
<fileset id="modifiedfiles" dir="src">
<modified/>
</fileset>
<target name="process-modified-dirs">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<groovy>
dirs = project.references.modifiedfiles.collect {
new File(it.toString()).parent
}
dirs.unique().each {
ant.echo("Do something with this dir: ${it}")
}
</groovy>
</target>
</project>
I'm trying to figure out how to set up a project using ant. For some reason, I can't get the junit tests to run. I set up a simple dummy project using ant to try and figure this out. All it has is a single unit test that should pass trivially.
My project structure looks like this.
.
|-- build.xml
|-- src
`-- test
|-- foo
| `-- MainTest.java
`-- junit-4.10.jar
MainTest.java looks like this.
package foo;
import org.junit.*;
import static org.junit.Assert.*;
public class MainTest {
#Test
public void passes() {
System.out.println("It works!");
}
}
And here is build.xml.
<project name="Nes" default="build" basedir=".">
<target name="build-test">
<javac srcdir="test">
<classpath>
<pathelement location="test/junit-4.10.jar" />
</classpath>
</javac>
</target>
<target name="test" depends="build-test">
<junit>
<classpath>
<pathelement location="test/junit-4.10.jar" />
</classpath>
<batchtest>
<fileset dir="test" includes="foo/MainTest.class" />
</batchtest>
</junit>
</target>
</project>
Here's the output I get from running ant test.
Buildfile: /home/hayden/dev/nes/build.xml
build-test:
[javac] /home/hayden/dev/nes/build.xml:4: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
test:
[junit] Test foo.MainTest FAILED
BUILD SUCCESSFUL
Total time: 0 seconds
I'm running ant 1.8.2 and Java 6.
What am I doing wrong?
You need to add a path to the actual .class file so JUnit can actually run the test. Since you are fully qualifying the test class name, you need to include the directory that contains the foo package:
<target name="test" depends="build-test">
<junit>
<classpath>
<pathelement location="test/junit-4.10.jar" />
<pathelement location="test" />
</classpath>
But I'd recommend that you change the javac task to not output build artifacts (i.e. .class files) into the same directory as your source files. Create a top-level "build" directory and put all your build output in there.
You should also quiet the first warning by adding a includeantruntime attribute to the javac task:
<javac srcdir="test" includeantruntime="false">
How to add to classpath all classes from set of directories?
I have following property:
class.dirs=lib1dir,lib2dir,lib3dir
There are classes under these directories.
Is it possible to add all classes under these directories to classpath?
Something like:
<classpath>
<dirset dir="${root.dir}" includes="${class.dirs}/**/*.class"/>
</classpath>
or
<classpath>
<pathelement location="${class.dirs}" />
</classpath>
But this example does not work, of course.
You can set up a path to include all .class files from your specific directories:
<path id="mypath">
<fileset dir="${root.dir}">
<include name="lib1dir/**/*.class lib2dir/**/*.class lib3dir/**/*.class"/>
</fileset>
</path>
However, if you want to use this path as a classpath, you only need to reference the root folders, otherwise you will get ClassNotFoundErrors as the package names translate into directories:
<path id="build.classpath">
<dirset dir="${root.dir}">
<include name="lib1dir lib2dir lib3dir"/>
</dirset>
</path>
Then reference the path by its id when using (e.g. for classpath):
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" />
I want to change the base directory of a FileList when referencing it later in my buildfile.
I define the following FileList:
<filelist dir="./dev" id="sourceFiles">
<file name="files/file.php" />
<file name="files/class.php" />
<file name="files/functions.php" />
</filelist>
And my targets are the following
<target name="fetch">
<copy todir="./src/files">
<filelist refid="sourceFiles" />
</copy>
</target>
<target name="build" depends="fetch">
<replaceregexp match="(\d+.\d+.\d+)(.\d+)?" replace="\1.${build.number}">
<filelist refid="sourceFiles" />
</replaceregexp>
</target>
So, when using the replaceregexp task, it will use the files located in ./dev - but I want the task to replace in the files copied earlier that are now located in ./src.
Of course, I could copy the file list and use another dir, but I'd very much like to only hold the list of files once in my buildfile.
How can I achieve this?
I think what you're looking for is a mapper within the copy command as follows:
<project name="demo" default="copy">
<property name="build.number" value="1.0.1"/>
<target name="copy">
<copy todir="build">
<fileset dir="src"/>
<regexpmapper from="^(.*)\.txt$" to="\1.${build.number}"/>
</copy>
</target>
<target name="clean">
<delete dir="build"/>
</target>
</project>
Here's my test files
|-- build
| `-- files
| |-- one
| | |-- file1.1.0.1
| | `-- file2.1.0.1
| `-- two
| `-- file3.1.0.1
|-- build.xml
`-- src
`-- files
|-- one
| |-- file1.txt
| `-- file2.txt
`-- two
`-- file3.txt