Change maven properties using Ant task - maven

I have set a maven property in the pom.xml.
<properties>
<build.start.date>someValue</build.start.date>
</properties>
Now I have an ant task doing the following:
<loadresource property="build.start">
<url url="http://someUrl?xpath=/*/id/text()"/>
</loadresource>
<property name="build.start.date" value="${build.start}"/>
<echo>Printing Ant Value ${build.start} </echo>
<echo>Printing Maven Value ${build.start.date}</echo>
This results in:
[echo] Printing Ant Value 2013-03-15_17-53-08
[echo] Printing Maven Value 2013-03-16
But I am expecting both to print:
[echo] Printing Ant Value 2013-03-15_17-53-08
[echo] Printing Maven Value 2013-03-15_17-53-08
I tried <loadresource property="build.start.date">
and
I tried <loadresource property="${build.start.date}">
So the question is how do I set a global maven property inside ant task?

I found the solution for this one.
First of all you need to have 1.7 version of antrun plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
....
</plugin>
Then under configuration you need to have exportAntProperties to true (false by default):
<configuration>
<exportAntProperties>true</exportAntProperties>

Related

Use maven-antrun-plugin to unzip a file using ${finalName} or wildcard (or regex)

I'm trying to unzip a .zip - File with a build-timestamp suffix appended using the maven-antrun-plugin. The var "finalName" is set in the effective pom.xml:
<finalName>XXXXXXX-server-5.20.0-SNAPSHOT-2022_11_29_14_45</finalName>
Using plugin version 3.1.0 (also tried 3.0.0 with the same outcome) it fails:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:3.1.0:run (Unzip Server) on project XXXXXXX-server: An Ant BuildException has occured: src 'C:\Dev\XXXXXXX-maven\XXXXXXX-server\target\${finalName}' doesn't exist.
[ERROR] around Ant part ...<unzip src="C:\Dev\XXXXXXX-maven\XXXXXXX-server\target/${finalName}" dest="C:/DEV/XXXXXXX/runOnline" />... # 7:99 in C:\Dev\XXXXXXX-maven\XXXXXXX-server\target\antrun\build-main.xml
Problem is clear: the variable finalName is not substituted(aka known).
...
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>Unzip Server</id>
<phase>package</phase>
<configuration>
<target>
<delete dir="${project.exec.directory}" includeemptydirs="true" />
<mkdir dir="${project.exec.directory}" />
<echo message="unzipping Server -${project.exec.directory}" />
<unzip src="${project.build.directory}/${finalname}" dest="${project.exec.directory}" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
...
Same snippet as above using wildcard-operator * does not work either:
<unzip src="${project.build.directory}/*.zip" dest="${project.exec.directory}" />
ends up not finding the *.zip (which actually exists in that directory at antrun execution time):
An Ant BuildException has occured: src 'C:\Dev\XXXXXXX-maven\XXXXXXX-server\target\*.zip' doesn't exist.
Any suggestions without using another plugin/mechanism?
Thank you :-)

maven error "Property ${samedir} has not been set"

How to fix the maven checkstyle plugin error
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.2:check (validate) on project yourproject:
Failed during checkstyle execution:
Failed during checkstyle configuration: unable to parse configuration stream:
com.puppycrawl.tools.checkstyle.api.CheckstyleException:
Property ${samedir} has not been set -> [Help 1]
${samedir} is a property working well with eclipse plugin and is required to get related files mentioned in the checkstyle configuration working well with eclipse IDE. So either I need a consistent replacement, or I can tell maven to define the property
Any ideas how to fix this issue?
You will have to set these values via the plugin configuration in your pom.
Example configuration of pom.xml if you have a directory named checks in your project root:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<configLocation>${basedir}/checks/checkstyle.xml</configLocation>
<propertiesLocation>${basedir}/checks/checkstyle_maven.properties</propertiesLocation>
</configuration>
</plugin>
And the checkstyle_maven.properties contents:
samedir=checks
As an alternative to the <propertiesLocation> you can also use the following, in which case you don't need the .properties file:
<propertyExpansion>samedir=${basedir}/checks</propertyExpansion>
This will make sure any configuration in checkstyle.xml will work like this:
<module name="Checker">
<module name="SuppressionFilter">
<property name="file" value="${samedir}/suppress.xml"/>
</module>
</module>
See for reference:
Using ${samedir} in Checkstyle-plugins for Gradle and Eclipse
http://rolf-engelhard.de/2011/04/using-the-same-suppression-filter-for-checkstyle-in-eclipse-and-maven/

Websphere 9 maven remote deployment

My springboot application builds into a WAR file (using Jenkins). I want to automate the remote deployment to Websphere 9.
I have read around and it seems there is no maven plugin for deployment to websphere 9 but ant support is pretty good. So, I'm using maven ant plugin to help running those ant tasks. I started with attempt to list the applications installed, just to see if it works. However I'm running into an exception related to localization:
[ERROR] C:\DEV\ant-was-deploy.xml:81:
java.util.MissingResourceException: Can't find bundle for base name
com.ibm.ws.profile.resourcebundle.WSProfileResourceBundle, locale
en_US
My ant-was-deploy.xml is referenced from pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>id123</id>
<phase>clean</phase>
<configuration>
<locales>es</locales>
<target>
<ant antfile="${basedir}/ant-was-deploy.xml">
<target name="listApps"/>
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
ant-was-deploy.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="websphere" default="listApps" basedir="." >
<target name="listApps" >
<taskdef name="wsListApps" classname="com.ibm.websphere.ant.tasks.ListApplications" classpath="${wasHome.dir}/plugins/com.ibm.ws.runtime.jar" />
<wsListApps
profileName="AppServ01"
wasHome="C:\\opt\\IBM\\WebSphere\\AppServer"
/>
</target>
</project>
I think the error comes from com.ibm.ws.runtime.jar. Inside it has WSProfileResourceBundle.class and WSProfileResourceBundle_en.class but not WSProfileResourceBundle_en_US.class (name is just an assumption - I have copied the bundle with this name inside the jar but it didn't work).
I also tried to set the locale for the entire plugin but it seems that localization for this plugin is not implemented properly (no impact in my case - I set the locale to 'es' but still got the error for en_US).
I also tried to pass system parameters to maven command: mvn clean -Duser.language=fr -Duser.country=FR
It didn't work either.
So, my question is if there is a way to change the locale before the ant script? If I can set it to 'en' probably it will find the right resource bundle.
I'm fairly new to Websphere, if there is another solution to automate the remote deployment to websphere 9 I would be happy to hear it. I would rather not use scripts on target server or Jenkins plugin but if there is no other way ...
I just had the same issue. In my case, i was using an AppServer name (AppSrv1 instead of AppSrv01) that did not exist anymore, in my maven settings.xml.
The right server name solved the issue.

How to override classpath for taskdef when using maven-antrun-plugin

I'm invoking an ant build from within a maven project using the maven-antrun-plugin. The ant build declares a taskdef and I need to set the classpath of the taskdef to be composed of the dependencies associated with the maven-antrun-plugin (i.e. maven.plugin.classpath) as well as some additional resources. I can only get the classpath to be maven.plugin.classpath.
A snippet of my pom.xml
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>test</phase>
<configuration>
<target>
<path id="my.classpath">
<pathelement location="src/test/resources" />
<path refid="maven.plugin.classpath" />
</path>
<property name="my_classpath" refid="my.classpath" />
<property name="plugin_classpath" refid="maven.plugin.classpath"/>
<echo message="my classpath: ${my_classpath}"/>
<echo message="plugin classpath: ${plugin_classpath}"/>
<!-- define the taskdef, specifying the classpathref from above -->
<taskdef name="sqlunit" classname="net.sourceforge.sqlunit.ant.SqlunitTask" classpathref="my.classpath"/>
<!-- invoke the tests -->
<sqlunit>
<fileset dir="src/test/db">
<include name="**/*.xml" />
</fileset>
</sqlunit>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
Inside of SQLUnit, I'm printing out the urls of the classpath:
ClassLoader cl = SQLUnit.class.getClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
System.out.println(url.getFile());
}
Executing "mvn test", I can see the my.classpath includes src/test/resources:
main:
[echo] my classpath: /Users/szalwinb/playpen/bszalwin/DbDmsApp/src/test/resources:/Users/szalwinb/.m2/repository/org/apache/maven/plugins/maven-antrun-plugin/1.8/maven-antrun-plugin-1.8.jar:/Users/szalwinb/.m2/repository/net/sourceforge/sqlunit/5.0/sqlunit-5.0.jar:/Users/szalwinb/.m2/repository/ant-contrib/ant-contrib/20020829/ant-contrib-20020829.jar:/Users/szalwinb/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/szalwinb/.m2/repository/org/jdom/jdom/1.1.3/jdom-1.1.3.jar:/Users/szalwinb/.m2/repository/postgresql/postgresql/8.4-701.jdbc4/postgresql-8.4-701.jdbc4.jar:/Users/szalwinb/.m2/repository/backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar:/Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.11/plexus-interpolation-1.11.jar:/Users/szalwinb/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar:/Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar:/Users/szalwinb/.m2/repository/org/apache/ant/ant/1.9.4/ant-1.9.4.jar:/Users/szalwinb/.m2/repository/org/apache/ant/ant-launcher/1.9.4/ant-launcher-1.9.4.jar
[echo] plugin classpath: /Users/szalwinb/.m2/repository/org/apache/maven/plugins/maven-antrun-plugin/1.8/maven-antrun-plugin-1.8.jar:/Users/szalwinb/.m2/repository/net/sourceforge/sqlunit/5.0/sqlunit-5.0.jar:/Users/szalwinb/.m2/repository/ant-contrib/ant-contrib/20020829/ant-contrib-20020829.jar:/Users/szalwinb/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/szalwinb/.m2/repository/org/jdom/jdom/1.1.3/jdom-1.1.3.jar:/Users/szalwinb/.m2/repository/postgresql/postgresql/8.4-701.jdbc4/postgresql-8.4-701.jdbc4.jar:/Users/szalwinb/.m2/repository/backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar:/Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.11/plexus-interpolation-1.11.jar:/Users/szalwinb/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar:/Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar:/Users/szalwinb/.m2/repository/org/apache/ant/ant/1.9.4/ant-1.9.4.jar:/Users/szalwinb/.m2/repository/org/apache/ant/ant-launcher/1.9.4/ant-launcher-1.9.4.jar
And when sqlunit task is executed, I see that the classpath only contains those artifacts found in maven.plugin.classpath.
/Users/szalwinb/.m2/repository/org/apache/maven/plugins/maven-antrun-plugin/1.8/maven-antrun-plugin-1.8.jar
/Users/szalwinb/.m2/repository/net/sourceforge/sqlunit/5.0/sqlunit-5.0.jar
/Users/szalwinb/.m2/repository/ant-contrib/ant-contrib/20020829/ant-contrib-20020829.jar
/Users/szalwinb/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar
/Users/szalwinb/.m2/repository/org/jdom/jdom/1.1.3/jdom-1.1.3.jar
/Users/szalwinb/.m2/repository/postgresql/postgresql/8.4-701.jdbc4/postgresql-8.4-701.jdbc4.jar
/Users/szalwinb/.m2/repository/backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar
/Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.11/plexus-interpolation-1.11.jar
/Users/szalwinb/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
/Users/szalwinb/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar
/Users/szalwinb/.m2/repository/org/apache/ant/ant/1.9.4/ant-1.9.4.jar
/Users/szalwinb/.m2/repository/org/apache/ant/ant-launcher/1.9.4/ant-launcher-1.9.4.jar
I looked at the source to AntRunMojo, and I see that is is building an Ant Project and adds maven.plugin.classpath as a reference to the project.
/* set maven.plugin.classpath with plugin dependencies */
antProject.addReference( "maven.plugin.classpath", getPathFromArtifacts( pluginArtifacts, antProject ) );
I think what is happening is that the taskdef is able to resolve its class using the references added to the project, so it ignores the classpathref associated with the taskdef element. To prove out this theory, I removed the classpathref attribute from the taskdef,
<taskdef name="sqlunit" classname="net.sourceforge.sqlunit.ant.SqlunitTask"/>
And the task was still able to resolve its class. And when the classloader printed out its resources, they were the ones defined by maven.plugin.classpath.
===============
I ended up changing the build for SQLUnit so that the log4j.properties file is included in the sqlunit.jar. Everything is happy now.

Where do maven goals go?

I'm using Maven 3.0.3. I want to write a simple task to copy a war file from my target directory to the Tomcat deploy directory. Where do I put my goal? I tried ...
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>socialmediaproxy</groupId>
<artifactId>socialmediaproxy</artifactId>
<packaging>war</packaging>
<version>0.1</version>
<goal name="copy-war" description="Copies the war file to the webapps directory">
<!-- This is Ant stuff -->
<copy file="${basedir}/target/${artifactId}-${version}.war" tofile="${warDestinationDir}"/>
</goal>
but when I run
mvn copy-war -P dev
I get this error ...
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project socialmediaproxy:socialmediaproxy:0.1 (/Users/davea/Documents/workspace-sts-2.6.0.SR1/socialmediaproxy/pom.xml) has 1 error
[ERROR] Malformed POM /Users/davea/Documents/workspace-sts-2.6.0.SR1/socialmediaproxy/pom.xml: Unrecognised tag: 'goal' (position: START_TAG seen ...y-war" description="Copies the war file to the webapps directory">... #9:84) # /Users/davea/Documents/workspace-sts-2.6.0.SR1/socialmediaproxy/pom.xml, line 9, column 84 -> [Help 2]
Any ideas? - Dave
The goal part in the pom does not exist anymore, cause that's from Maven 1...but you defined a pom (model version 4.0.0) which is intended for Maven 3. Take a look into the current reference for the pom.
You can use ANT itself to copy the stuff from your target directory to the Tomcat Dir
Since you want to move the files from the target dir, run the ANTRUN plugin in the package phase.
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<phase>package</phase>
<configuration>
<target>
<copy file="${basedir}/target/${artifactId}-${version}.war" tofile="${warDestinationDir}"/>
</target>
</configuration>
<goals>
<goal>run</goal>
This will run the ANT task mentioned in <target> after executing the plase goal.

Resources