I wrote a test where I am importing a class from package com/intel/epgsw/JunitAdapter.groovy
When I try to run the test, I get this error:
[ERROR] Failed to execute goal org.codehaus.gmavenplus:gmavenplus-plugin:1.5:testCompile (groovy) on project jenkinsfile-test-shared-library: Error occurred while calling a method on a Groovy class from classpath. InvocationTargetException: startup failed:
[ERROR] src/test/groovy/CloneTestSpec.groovy: 3: unable to resolve class src.com.intel.epgsw.JunitAdapter
[ERROR] # line 3, column 1.
[ERROR] import com.intel.epgsw.JunitAdapter
[ERROR] ^
Test file is present in: src/test/groovy
Class that needs to be imported: com/intel/epgsw/JunitAdapter.groovy
My test file is CloneTestSpec.groovy
Here is the tree :
src
│ ├── com
│ │ └── intel
│ │ ├── epgsw
│ │ │ ├── TestResultEnum.groovy
│ │ │ ├── **JunitAdapter.groovy**
│ │ │
│ └── test
│ ├── com
│ │ └── intel
│ │ └── epgsw
│ ├── epgsw
│ │ └── FooBar98.groovy
│ ├── groovy
│ │ ├── **CloneTestSpec.groovy**
This is a section of my pom.xml
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<configuration>
<testSourceDirectory>src/test/groovy</testSourceDirectory>
<includes>
<include>**/*Spec.java</include>
<include>**/*Spec.class</include>
<include>**/*Spec</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${groovy.gmaven.pluginVersion}</version>
<executions>
<execution>
<id>groovy</id>
<goals>
<goal>addTestSources</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>
<directory>src</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</source>
</sources>
<testSources>
<testSource>
<directory>src/test/groovy/com/intel/epgsw</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</testSource>
</testSources>
</configuration>
</plugin>
</plugins>
Your directory layout is chaotic. Please move all your test classes into the src/test/groovy base folder. There, you create a directory structure according to your package structure. For example (I cannot say for sure without seeing the package declarations in each file), assuming that
all application classes are Java classes,
all test classes are Groovy classes and
all classes are in the exact same package com.intel.epgsw:
src
main
java
com
intel
epgsw
ApplicationClassA.java
ApplicationClassB.java
test
groovy
com
intel
epgsw
TestResultEnum.groovy
JunitAdapter.groovy
FooBar98.groovy
CloneTestSpec.groovy
If any of my above assumptions are false, you need to adjust the directory structure accordingly. But your chaotic directory layout with 3 different base directories causes the trouble you are facing. Please do learn some Java, Groovy and Maven basics, especially the Maven standard directory layout.
Related
I have a list of files in a directory . I have to read all the file names and extract the file name in a sorted order. How can I do that using ant script
You did not specify the sort criteria. Lots of options available see the resource documentation in the ANT manual
https://ant.apache.org/manual/Types/resources.html
Example
├── build.xml
└── src
├── data1
│ ├── file1.txt
│ ├── file2.txt
│ └── file3.txt
├── data2
│ ├── file4.txt
│ └── file5.txt
└── data3
└── file6.txt
Running the project prints the list of files sorted in reverse order based on modified date
build:
[echo] Files: /../src/data2/file4.txt:/../src/data3/file6.txt:/../src/data2/file5.txt:/../src/data1/file3.txt:/../src/data1/file2.txt:/../src/data1/file1.txt
build.xml
<project name="demo" default="build">
<target name="build">
<sort id="src.files">
<fileset dir="src" />
<reverse xmlns="antlib:org.apache.tools.ant.types.resources.comparators">
<date />
</reverse>
</sort>
<pathconvert targetos="unix" property="srcFiles" refid="src.files"/>
<echo message="Files: ${srcFiles}"/>
</target>
</project>
I have a folder sources with a subfolder be under it. Next fileset should select all *.cls files under the be folder, recursively! (so also .cls files in subfolders of the be folder):
<fileset dir="${basedir}/sources">
<include name="be/**/*.cls" />
</fileset>
Apparently ant doesn't select a single file...
If I change it to
<fileset dir="${basedir}/sources/be">
<include name="**/*.cls" />
</fileset>
Ant selects all of the .cls files.
What's the difference between the two snippets?
Folder structure:
sources
be
dirA
dirB
.cls files
dirC
.cls files
They both copy the files, just in different ways.
Notice how in the "be" directory root is included in the first copy:
├── build.xml
├── sources
│ └── be
│ └── dirA
│ ├── dirB
│ │ ├── file1.cls
│ │ └── file2.cls
│ └── dirC
│ └── file3.cls
└── target
├── copy1
│ └── be
│ └── dirA
│ ├── dirB
│ │ ├── file1.cls
│ │ └── file2.cls
│ └── dirC
│ └── file3.cls
└── copy2
└── dirA
├── dirB
│ ├── file1.cls
│ └── file2.cls
└── dirC
└── file3.cls
build.xml
<project name="demo" default="copy">
<property name="src.dir" location="sources"/>
<property name="build.dir" location="target"/>
<target name="copy" depends="copy1,copy2">
</target>
<target name="copy1">
<copy todir="${build.dir}/copy1">
<fileset dir="${src.dir}">
<include name="be/**/*.cls" />
</fileset>
</copy>
</target>
<target name="copy2">
<copy todir="${build.dir}/copy2">
<fileset dir="${src.dir}/be">
<include name="**/*.cls" />
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
</project>
I am using a third party build script creates minified js files with the minified file called 'someFile.js' and an unminified version called 'someFile.js.uncompressed.js'. I need one of my tasks in the build.xml to copy only the js files that have a partner '.uncompressed.js' file to another location. For example, given a directory structure like this:
- rootDirectory
- minified.js
- minified.js.uncompressed
- unminified.js
- anotherDirectory
- anotherMinified.js
- anotherMinified.js.uncompressed.js
- unminified.js
- anotherUnminified.js
the destination directory should end up like this:
- rootDirectory
- minified.js
- anotherDirectory
- anotherMinified.js
Is there any way to accomplish this with ant? I am using ant 1.8.1.
Thanks in advance for any help!
You can use an Ant <present> selector to do this. For example:
<copy todir="dest">
<fileset dir="src">
<present targetdir="src">
<mapper type="glob" from="*" to="*.uncompressed.js" />
</present>
</fileset>
</copy>
In this case the targetdir in the selector is the same as the root directory of the fileset.
What this does is copy any file in the src directory tree where a file with the same name, with .uncompressed.js appended also present.
You could use a custom scriptselector for your fileset.
Example
├── build.xml
├── src
│ └── rootDirectory
│ ├── anotherDirectory
│ │ ├── anotherMinified.js
│ │ ├── anotherMinified.js.uncompressed
│ │ ├── anotherUnminified.js
│ │ └── unminified.js
│ ├── minified.js
│ ├── minified.js.uncompressed
│ └── unminified.js
└── target
└── rootDirectory
├── anotherDirectory
│ └── anotherMinified.js
└── minified.js
build.xml
<project name="demo" default="copy-files">
<target name="copy-files">
<copy todir="target">
<fileset dir="src">
<scriptselector language="javascript">
importClass(java.io.File);
var testForFile = new File("src/" + filename + ".uncompressed");
self.setSelected(testForFile.exists());
</scriptselector>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="target"/>
</target>
</project>
Notes:
This should work without the need for additional jars. Javascript is supported by modern JVMs.
I have a source directory with the following structure.
D:\Files\temporaryname\my_files_to_be_copied
The directory name "temporaryname" may vary from time to time. I need an ant script to copy the files "my_file_to_be_copied" to some other destination.
Simply I need to go one folder inside the source directory and start copying all. Please assist. Thanks in advance.
Lots of ways to do this here's one example, demonstrating filesets and mappers.
Example
├── build.xml
├── build
│ └── destination
│ ├── file1.txt
│ ├── file2.txt
│ ├── file3.txt
│ └── file4.txt
└── src
├── dir1
│ └── my_files_to_be_copied
│ ├── file1.txt
│ └── file2.txt
└── dir2
└── my_files_to_be_copied
├── file3.txt
└── file4.txt
build.xml
<project name="demo" default="copy">
<target name="copy" description="Copy files">
<copy todir="build/destination">
<fileset dir="src" includes="**/my_files_to_be_copied/**"/>
<cutdirsmapper dirs="2"/>
</copy>
</target>
<target name="clean" description="Additionally purge ivy cache">
<delete dir="build"/>
</target>
</project>
In ivy I can set a retrieve pattern in order to copy all my dependencies somewhere I want to.
For example:
<ivy:retrieve pattern="${local-maven2-dir}/[organisation]/[module]/[revision]/[module]-[revision].[ext]" conf="compile" type="jar,bundle" sync="true"/>
I wonder is it possible to treat organization not as a folder, but as a set of nested folders, and keep in deepest folder (which is revision) the jar package, just like jars are stored in maven default repo.
So, basically I want to have jars located in paths like
com/yahoo/platform/yui/yuicompressor/2.4.7
and not like
com.yahoo.platform.yui/yuicompressor/2.4.7
PS: involving groovy scripting counts as a valid solution as well, it's just that I have no idea how can groovy be involved here.
Actually, it's quite easy and already documented in Ivy (look near the bottom of the page). You can use [orgPath]:
<ivy:retrieve conf="compile"
type="jar,bundle"
sync="true"
pattern="${local-maven2-dir}/[orgPath]/[module]-[revision].[ext]"/>
The following example uses groovy.
David W. offers a far simpler solution, relying on a new "orgPath" pattern token introduced in ivy 2.3.
Example
Produces the following output
├── build
│ ├── com
│ │ └── yahoo
│ │ └── platform
│ │ └── yui
│ │ └── yuicompressor
│ │ └── 2.4.7
│ │ └── yuicompressor-2.4.7.jar
│ └── rhino
│ └── js
│ └── 1.6R7
│ └── js-1.6R7.jar
├── build.xml
└── ivy.xml
build.xml
<project name="demo" default="retrieve" xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="resolve">
<ivy:resolve/>
<ivy:cachepath pathid="build.path" conf="build"/>
</target>
<target name="retrieve" depends="resolve">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<ivy:artifactproperty conf="compile" name="index.[module].[artifact]" value="[module].[artifact]"/>
<ivy:artifactproperty conf="compile" name="[module].[artifact].organisation" value="[organisation]"/>
<ivy:artifactproperty conf="compile" name="[module].[artifact].module" value="[module]"/>
<ivy:artifactproperty conf="compile" name="[module].[artifact].artifact" value="[artifact]"/>
<ivy:artifactproperty conf="compile" name="[module].[artifact].revision" value="[revision]"/>
<ivy:artifactproperty conf="compile" name="[module].[artifact].ext" value="[ext]"/>
<ivy:artifactproperty conf="compile" name="[module].[artifact].cachefile" value="${ivy.cache.dir}/[organisation]/[module]/jars/[artifact]-[revision].[ext]"/>
<groovy>
modules = properties.findAll { it.toString().startsWith("index.") }
modules.each { key, value ->
def organisation = properties[value+".organisation"].replace(".","/")
def module = properties[value+".module"]
def artifact = properties[value+".artifact"]
def revision = properties[value+".revision"]
def ext = properties[value+".ext"]
def cachefile = properties[value+".cachefile"]
ant.copy(file:cachefile, tofile:"build/${organisation}/${module}/${revision}/${artifact}-${revision}.${ext}")
}
</groovy>
</target>
</project>
ivy.xml
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="build" description="Build dependencies"/>
<conf name="compile" description="Compile classpath"/>
</configurations>
<dependencies>
<!-- build dependencies -->
<dependency org="org.codehaus.groovy" name="groovy-all" rev="2.1.1" conf="build->default"/>
<!-- compile dependencies -->
<dependency org="com.yahoo.platform.yui" name="yuicompressor" rev="2.4.7" conf="compile->default"/>
</dependencies>
</ivy-module>