Ant - Zip Multiple Folders - maven

I'm trying to run an Ant job within Maven to look at a folder, and based on how many folders are in that folder, to create x number of zip files with the name of the folder. I have it working manually, but it would be nice if I didn't have to edit the pom each time I added a new folder to this structure.
<configuration>
<target name="zip">
<zip destfile="root/sub1/sub1.jar">
<zipfileset dir="root/sub1/unpacked/" includes="**" ></zipfileset>
</zip>
</target>
</configuration>
If I were to add sub2 to the root dir, I would like it to be picked up automagically, and create a sub2.jar file (yes, I'm aware I'm using .jar, but the program that is taking these files expects .jar files, but they're not jar files in that they contain any Java code, they're just zip files with jar extensions)
I've tried this
Thanks I had a look at the first link, but perhaps I'm doing it wrong
<target name="checkDir">
<foreach target="zip" param="theFile">
<dirset dir="root" casesensitive="yes">
<include name="**"/>
</dirset>
</foreach>
</target>
<target name="zip">
<!-- <zip destfile="root/${theFile}/${theFile}.jar">
<zipfileset dir="root/${theFile}/unpacked/" includes="**" ></zipfileset>
</zip> -->
<echo message="${theFile}"/>
</target>
I just get
[INFO] --- maven-antrun-plugin:1.7:run (process-javascript-plugin) # war---
[INFO] Executing tasks
zip:
[echo] ${theFile}
[INFO] Executed tasks
Still doesn't seem to be working.
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>process-cartridges</id>
<phase>compile</phase>
<configuration>
<target>
<ant antfile="root/build-main.xml"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
build-main.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project>
<target name="checkDir">
<foreach target="zip" param="theFile">
<dirset dir="root" casesensitive="yes">
<include name="**"/>
</dirset>
</foreach>
</target>
<target name="zip">
<zip destfile="root/${theFile}/${theFile}.jar">
<zipfileset dir="root/${theFile}/unpacked/" includes="**" />
</zip>
<echo message="TESTZIP ${theFile}"/>
</target>
</project>
Doesn't seem to be working. Am I missing anything?

You could use the foreach task of Ant to do this, combining the answers from those two posts: Ant: How to execute a command for each file in directory? and Calling foreach in maven-antrun-plugin.
However, you can also have a Maven solution using the iterator-maven-plugin to iterate over all sub-folders and then use the maven-jar-plugin to make the jar:
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>0.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>iterator</goal>
</goals>
<configuration>
<folder>root/</folder>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
</plugin>
<goal>jar</goal>
<configuration>
<classesDirectory>root/#item#/unpacked</classesDirectory>
<outputDirectory>root/#item#</outputDirectory>
<finalName>#item#.jar</finalName>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
This is bound to the package phase. It will iterate over all direct subfolders of the root folder and invoke the jar plugin with the given configuration. #item# is used to refer to the current directory name.

Related

How to set ANTLR3 extended option -Xmaxinlinedfastates in maven configuration?

Is it possible to set the max DFA states before table used rather than inlining in the configuration of the plugin maven antlr3 like the extended option -Xmaxinlinedfastates in the command line of antlr.Tool ?
I find it for ant script but nothing with maven.
If yes, how to ?
Thanks.
Thanks to Jiri Tousek.
I found an alternative to what I call a gap in the maven antlr3 plugin. Using the maven antrun plugin allowed me to solve this problem.
Here is the statement I made in pom.xml and that works for me:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<target>
<!-- Place any Ant task here. You can add anything you can add between
<target> and </target> in a build.xml. -->
<java classname="org.antlr.Tool" fork="true"
failonerror="true">
<arg value="-verbose" />
<arg value="-Xmaxinlinedfastates"/>
<arg value="10"/> <!-- Value for the exended option -Xmaxinlinedfastates -->
<arg path="./src/main/antlr3/*.g" />
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Axis wsdl2java maven plugin with xmlbeans doesn't include resources folder

I'm using axis2 to re-generate the client code for an updated webservice that I need to use, for a legacy application.
Being a legacy application I would like to avoid changing the code that has been already written, and re-generate the classes as they were generated who-know-how-many years ago by the eclipse plugin, but this time using maven instead of eclipse.
So I seen that they were generated using axis2 and xmlbeans, and I produced the configuration in the maven plugin:
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.5.6</version>
<executions>
<execution>
<id>TheirsWs</id>
<goals>
<goal>wsdl2code</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<packageName>it.theirs.ws</packageName>
<wsdlFile>${basedir}/src/main/resources/theirWs.wsdl</wsdlFile>
<generateServerSide>false</generateServerSide>
<databindingName>xmlbeans</databindingName>
<unpackClasses>true</unpackClasses>
</configuration>
</execution>
</executions>
</plugin>
What happen now is a very nice thing.
The plugin generate a .class file in the generated-sources / axis2 / wsdl2code / resource folder, However it is not added by maven to the final package, causing a ClassNotFoundException when calling the webservice.
I solve the problem by adding the resource folder into the JAR using the maven default feature of including and excluding folder. The solution for your case will be:
<build>
<!-- This will the MAVEN to copy the entire folder, you can copy only the .class files -->
<resources>
<resource>
<directory>${project.build.directory}/generated-src/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.5.6</version>
<executions>
<execution>
<id>TheirsWs</id>
<goals>
<goal>wsdl2code</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<packageName>it.theirs.ws</packageName>
<wsdlFile>${basedir}/src/main/resources/theirWs.wsdl</wsdlFile>
<generateServerSide>false</generateServerSide>
<databindingName>xmlbeans</databindingName>
<!-- I add this line just to be easy to referenciate the souce -->
<outputDirectory>${project.build.directory}/generated-src</outputDirectory>
<unpackClasses>true</unpackClasses>
</configuration>
</execution>
</executions>
</plugin>
Had the same problem, i changed the Ant build.xml buildfile from:
<target depends="pre.compile.test" name="compile.src" if="jars.ok">
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}" srcdir="${src}">
<classpath refid="axis2.class.path"/>
</javac>
</target>
To:
<target depends="pre.compile.test" name="compile.src" if="jars.ok">
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}" srcdir="${src}">
<classpath refid="axis2.class.path"/>
</javac>
<copy todir="${classes}">
<fileset dir="${resources}"/>
</copy>
</target>
The new Copy task add all the resources to the target classes folder so the generated Jar will include them.
Hope it helps.

What is a better way for checking for the existence of an environment variable in the Maven antrun plugin?

I’m using Maven 3.2.3. I have this for my ant run plugin …
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>20020829</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>create-dodeploy-file</id>
<phase>package</phase>
<configuration>
<target>
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<property environment="env"/>
<if>
<isset property="env.JBOSS_HOME"/>
<then>
<echo file="${JBOSS_HOME}/standalone/deployments/${project.artifactId}.war.dodeploy" append="false" message="" />
</then>
</if>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Despite the fact I know $JBOSS_HOME is defined in my environment (I’m able to run
echo $JBOSS_HOME
from the same bash shell in which I run my Maven build), the “isset” directive always returns false. Is there a better way to test for the existence of an environment variable in the Antrun plugin?
A target also has the ability to perform its execution if (or unless) a property has been set. To make a target sense this property, you should add the if (or unless) attribute with the name of the property that the target should react to.
<target name="build-if" if="env.JBOSS_HOME"/>
<target name="build-unless" unless="env.JBOSS_HOME"/>

Is there a maven plugin to install the project?

I'm new with Maven, I just finished to read the Sonatype guide and I'm very satisfied by the functions that maven makes available. I created an application distribution (.zip) package and I would like to know if there is a manner to use maven as an installer.
I don't mean the installation that maven does into the local repository, what I mean is explained by the following example:
I've a folder with a jar file, an .sql script, a /lib and obviously a pom.xml file.
I would like that maven installs this project for me when I execute the "mvn" command.
So maven should:
- Copy the jar file in the ${TOMCAT_HOME}\webapps directory.
- Execute the sql script on the postgresql database
- Copy the \lib directory in c:\myLibs
- etc etc
During this process it should also make some checks (example TOMCAT_HOME is set on the system? Postgres is turned on? etc.) and ask to the user some parameter (example "The installation will reset the database do you want to continue?" or "Please insert the database password: ".
Is there a maven plugin that helps to do this? If not is there an application alike maven specialized to create "installer" ? Is it a standard, widespread application?
Thanks a lot for your help!
Try:
mvn deploy.
Add to your pom
<distributionManagement>
<repository>
<id>sonatype.internal</id>
<name>Internal Release Repository</name>
<url>http://sonatypeAddress:sonatypePort/context</url>
</repository>
</distributionManagement>
Plugins section:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<configuration>
<tag>${build.tag}</tag>
<username>${scm.username}</username>
<password>${scm.password}</password>
</configuration>
</plugin>
antrun plugin - and make what you want.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<dependencies>
...
</dependencies>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<configuration>
<target>
<delete dir="mylocalization" />
<copy file="target/out/my.jar" tofile="mylocalication" />
<copy todir="mylocalization/doc">
<fileset dir="target/doc" />
</copy>
<copy todir="mylocalization/somethingMore">
<fileset dir="target/more">
<include name="a.txt" />
<include name="b*.txt" />
</fileset>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
See also maven-wagon

Maven and Ant plugin to copy resources intelligently

It's easy to use org.apache.maven.plugins:maven-antrun-plugin to copy resources and rename them but is there a way to do this intelligently with wildcards or other mecanisms to conform personnal rules ?
For example if I have those files :
${project.artifactId}-${project.version}.swf
a.swf
b.swf
...
z.swf
I would like to copy all those files inside directory and rename only ${project.artifactId}-${project.version}.swf to foo.swf. I know I can use :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>copy-swf-files</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="copy swf files to web project">
<copy file="${project.build.directory}/${project.artifactId}-${project.version}.swf" tofile="${swf.output.location}/foo.swf" />
<copy file="${project.build.directory}/a.swf" tofile="${swf.output.location}/a.swf" />
<copy file="${project.build.directory}/z.swf" tofile="${swf.output.location}/z.swf" />
</target>
</configuration>
</execution>
</executions>
</plugin>
it's work but is there another convenient way to do that because if I have 1000 files to copy, it will be very boring...Thanks
Do you know Ant? All the Maven Ant plugin is doing is calling Ant with the tasks you list. You can do any Ant task including <taskdef>, etc.
What it looks like you're doing can be done with fileset:
<copy todir="${swf.output.location}">
<fileset dir="${project.build.directory}">
<include name="*.swf"/>
</fileset>
</copy>
This will copy all *.swf files located directly under in the ${project.build.directory} (and no subdirectories) to the ${swf.output.location} directory. If you want to copy the entire directory tree of *.swf files, just change the <include>:
<copy todir="${swf.output.location}">
<fileset dir="${project.build.directory}">
<include name="**/*.swf"/> <!--NOTE DIFFERENCE HERE-->
</fileset>
</copy>
If you need to munge the file names, you can use Mappers. The simplest mapper would be the flatten mapper:
<copy todir="${swf.output.location}">
<fileset dir="${project.build.directory}">
<include name="**/*.swf"/> <!--NOTE DIFFERENCE HERE-->
</fileset>
<mapper type="flatten"/>
</copy>
This will copy the entire directory tree and flatten all the files into a single directory. There are mappers that can match globs, regular expressions, and even scripting.
The <include> is a selector and not a mapper because it selects what files you want to act upon. These too can be quite complex, and you can match file based upon their names iva regular expressions, or even their contents.
I'm surprised there isn't a Maven plugin that allows you to do this.
Ok I finally used this plugin to do this :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>copy-swf-files</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="copy swf files to web project">
<copy todir="${swf.output.location}">
<fileset dir="${project.build.directory}" includes="*.swf">
<filename regex="^sim-flex.*"/>
</fileset>
<mapper type="regexp" from=".*" to="sim.swf"/>
</copy>
<copy todir="${swf.output.location}" >
<fileset dir="${project.build.directory}" includes="*.swf">
<not>
<filename regex="^sim-flex.*"/>
</not>
</fileset>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
Use maven-assembly-plugin with assembly descriptor of "dir" "formats/format", a "fileSets/fileSet" which will copy majority of swf's excluding the special swf, and a "files/file" which will copy that one swf to a file with different name.

Resources