Maven regular expression to match file is not working - maven

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

Related

Maven antrun move not deleting source file

Developing on Windows 10 I have a Java project in Maven that has a Linux "launcher" shell script for the FooBar utility stored in the repository at src/bin/foobar.sh. It uses resource filtering to substitute in the correct executable JAR path so that what gets built is a foobar.sh script that launches the executable JAR in the same directory.
The POM uses org.apache.maven.plugins:maven-antrun-plugin:1.8 to enable the executable flag on the foobar.sh script in the target/bin directory (which has been already been copied using Maven resource filtering, with that directory path stored in the ${binOutputDirectory} property):
<chmod dir="${binOutputDirectory}" includes="**/*.sh" perm="+x" />
Then it renames the foobar.sh file to simply foobar (i.e. it removes the extension) to follow best practices for shell scripts:
<move todir="${binOutputDirectory}">
<fileset dir="${binOutputDirectory}">
<include name="**/*.sh" />
</fileset>
<mapper type="glob" from="*.sh" to="*" />
</move>
You can see e.g. globalmentor-root pom.xml at c31ae410143f86ebf2bf10467214214d87b2eb61 for the full POM source code. Actual child POMs will simply enable the AntRun operations by providing their executions an appropriate phase like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>set-shell-scripts-executable</id>
<phase>process-resources</phase>
</execution>
<execution>
<id>remove-shell-script-extensions</id>
<phase>process-resources</phase>
</execution>
</executions>
</plugin>
The essential part of that is working fine, and I wind up with a foobar file in my distributable ZIP file, with its executable flag enabled as desired. Unfortunately I also wind up with the original foobar.sh file as well, and I can see in target/bin (where the .sh extension gets removed) that both files are there as well. So it would appear that AntRun <move> is behaving as <copy>.
To see this in action, build the Guise Mummy 0.1.0 project and look in the cli/target/bin directory; you'll see that guise.sh has not been deleted.
To work around the problem, I can add an extraneous <delete> command; this will successfully remove foobar.sh. (The difference in <fileset> syntax is irrelevant; I switched only because it was more concise.)
<move todir="${binOutputDirectory}">
<fileset dir="${binOutputDirectory}" includes="**/*.sh"/>
<mapper type="glob" from="*.sh" to="*" />
</move>
<delete>
<fileset dir="${binOutputDirectory}" includes="**/*.sh"/>
</delete>
Why is AntRun <move> by itself not removing the original target/bin/foobar.sh file after it copies it to target/bin/foobar as part of the move operation?
Upgrading to org.apache.maven.plugins:maven-antrun-plugin:3.1.0 seems to have fixed the problem. When I created this question I had been using v1.8. I can only suppose that org.apache.maven.plugins:maven-antrun-plugin:1.8 is buggy.
Noticed in the pom, within the antrun goals, you are modifying permissions of the .sh script. This does not confirm if the shell script is writeable, it may be read-only:
<execution>
<id>set-shell-scripts-executable</id>
<!--
Enable execute permission for the shell scripts in `${binOutputDirectory}`.
Enable by specifying a phase (e.g. `process-resources`) in child POM.
-->
<phase>none</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<chmod dir="${binOutputDirectory}" includes="**/*.sh" perm="+x" />
</target>
</configuration>
</execution>
Try the following, apply the overwrite attribute, like so:
(overwrite overwrite existing files even if the destination files are newer)
<move todir="${binOutputDirectory}" overwrite="true">
<fileset dir="${binOutputDirectory}">
<include name="**/*.sh" />
</fileset>
<mapper type="glob" from="*.sh" to="*" />
</move>
If that does not work, also add the force attribute, like so:
(force Overwrite read-only destination files)
<move todir="${binOutputDirectory}" overwrite="true" force="true">
<fileset dir="${binOutputDirectory}">
<include name="**/*.sh" />
</fileset>
<mapper type="glob" from="*.sh" to="*" />
</move>

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)

srcdir attributes must be set

Im trying to migrate my code from ant to gradle and imported my build.xml in build.gradle and some of my tasks work fine.
However there are some tasks when i run them it says
Execution failed for task ':compile'.
srcdir attribute must be set!
Pasting a snippet of my Build.xml and the compile task below is failing.
<project basedir="." default="deploy" name="FitNesse">
<property name="fitnesse.target" value="/build/test-fitnesse"/>
<patternset id="test.patternset">
<include name="**/*Test*.java"/>
</patternset>
<propertyset id="test.propertyset">
<propertyref name="basedir"/>
<propertyref name="test.patternset"/>
</propertyset>
<fileset id="classpath.fitnesse" dir="./libs">
<include name="antlr-2.7.7.jar"/>
<include name="asm-4.1.jar"/>
<include name="asm-analysis-4.1.jar"/>
<include name="asm-commons-4.1.jar"/>
<include name="asm-tree-4.1.jar"/
><target name="compile" description="Compile java sources" depends="clean">
<mkdir dir="build/classes"/>
<javac includeantruntime="false"/>
<javac srcdir="src/main/java" destdir="build/classes" debug="on">
<classpath refid="classpath.compile"/>
<compilerarg value="-processorpath"/>
<compilerarg value="-AmethodConstraintsSupported=true"/>
</javac>
</target>
Source folder structure
/src/main/java/com/xebia/inc/xeb
I had changed my src folder structure from ant "src" to a gradle project "/src/main/java/com/xebia/incubator/xebium". and lib folder to libs
Please help. I'm kind of novice to this.
Thanks,
The full error message should give you the line number and it's most probably the line that contains
<javac includeantruntime="false"/>
which looks like a copy-paste error.

How to create sitemap.xml from docbook source

I need to generate sitemap.xml in the standard Google acceptable format from my docbook source. I'm currently using gradle to power the build process.
Is there an existing tool out there to generate sitemap.xml automatically as part of the build?
By Using Ant from Gradle is seams to be possible as there is an Apache Ant Task for generating a XML Sitemap. Read the manual at GitHub, for the usage details.
<target name="generate_sitemap" description="generates the sitemap">
<taskdef classname="uk.co.arjones.ant.task.Sitemap" name="sitemap"></taskdef>
<sitemap url="http://organisation.org" gzip="yes" lastmod="now" destdir="${BUILD_DIR}">
<fileset dir="${BUILD_DIR}">
<include name="**.docbook"></include>
<include name="**.dbx"></include>
<exclude name="google*"></exclude>
</fileset>
</sitemap>
</target>

Apache Ant JAR Task: not finding properties

How exactly should I specify the location of all the properties files inside the ant manifest?
My jar is not working because it can't find the log4j, Spring, etc properties.
These files are all contained within a folder called "server-config" that sits at the same level as the source code, ie:
META-INF
com
server-config
Essentially, I want to know what I need to add to the Class-Path property for the jar to be aware of all these properties files inside the server-config folder.
Here's my current task:
<jar destfile="${root.home}/onejar/build/main/main.jar" basedir="${build.home}">
<manifest>
<attribute name="Class-Path" value=".;server-config" />
</manifest>
<include name="com/mycompany/client/*"/>
<include name="com/mycompany/portable/util/*"/>
<include name="com/mycompany/request/*"/>
<include name="com/mycompany/model/*"/>
<include name="com/mycompany/controller/*"/>
<include name="com/mycompany/helpers/*"/>
<include name="server-config/*"/>
</jar>
I've tried a few things and none of them are working, I keep on getting errors due to the file not being found.
Any help would be greatly appreciated!
You can remove the entire <manifest... part - that's not what the Class-Path manifest attribute does. It's for things external to the JAR.
The line <include name="server-config/*"/> should work - if the server-config directory exists inside your ${build.home} directory. You probably need a task to copy them there - you mention that the source code sits at the same level, but you don't mention where they are compiled to.
An example -
<mkdir dir="${build.dir}/server-config"
<copy todir="${build.dir}/server-config">
<fileset dir="${src.dir}/server-config">
<include name="*"/>
</fileset>
</copy>

Resources