Maven 2 export jar to seperate directory - maven-2

I need to export all the compiled class files as a jar to a directory of a local testserver. I would like to let maven do this automatically.
<!-- Build instructions -->
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>compile package</defaultGoal>
<sourceDirectory>${basedir}/src/</sourceDirectory>
<outputDirectory>/home/...</outputDirectory> <!--only class files go here, not the jar-->
<resources>
<resource>
<includes>
<include>plugin.yml</include>
</includes>
<directory>${basedir}</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
To build I first clean and then package via IntelliJ's Maven plugin. When I package I would like it to package the jarfile to my testserver so I don't have to move it over manually after every build.

Use Maven Ant plugin to custom code what you want to after a particular goal:
Using Ant with Maven
Ant Copt Task

You need to configure maven-jar-plugin and configure its outputDirectory as per your requirements, you can see the details on this link.
http://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html

Related

How to run equivalent of Mule studio "Export" in a script?

I have the need to create a deployable ZIP archive from a script, much as the "Export" function does in Mule Studio. I'm expecting the ZIP to contain everything needed to deploy the app: JAR files, message flows, etc etc etc - again, just as Mule Studio Export does.
Is there a simple way to do this, or an example I can follow?
Maven is your best bet. Using the mule plugin for maven runnning mvn package will produce the deployable archive. More info on Mule and Maven here: http://www.mulesoft.org/documentation/display/current/Using+Maven+with+Mule
There is also an ant plugin but don't use it myself. Some info here: http://blogs.mulesoft.org/building-mule-apps-with-ant/
Alternatively, you can read about the application deployment structure here: http://www.mulesoft.org/documentation/display/current/Application+Format , so in theory you can build this structure yourself. But I wouldn't advise it and would stick to Maven.
Use Maven for building the app in jar/zip and deploy to the Mule standalone server automatically ... Make Sure your MULE_HOME is set to your standalone server in Environment variable ... and insert the following in your Maven Script to build it in Jar/Zip and deploy to app folder of Mule Standalone directly :-
<build>
<resources>
<resource>
<directory>>${project.basedir}/lib</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.mule.tools</groupId>
<artifactId>maven-mule-plugin</artifactId>
<version>1.6</version>
<extensions>true</extensions>
<configuration> <!-- This tag sets true/false to copy jar/zip file to Mule server app folder -->
<copyToAppsDirectory>true</copyToAppsDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>ISO-8859-1</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>YOUR_APPPLICATION_NAME</finalName>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>${eclipsePluginVersion}</version>
<configuration>
<!-- by default download all sources when generating project files -->
<downloadSources>true</downloadSources>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER/${vmtype}/${jdkName}
</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
<!--
make sure that MULE_HOME is set when building (required below when copying the
artifact to Mule's apps directory
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0-beta-1</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>env.MULE_HOME</property>
<message>You must set MULE_HOME before installing the example.</message>
</requireProperty>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>package-example</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!-- This tag automatically deploy jar/zip file to server -->
<copy file="${project.build.directory}/${project.build.finalName}.zip"
todir="${env.MULE_HOME}/apps" overwrite="true"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

yuicompressor-maven-plugin not bounded to process-resources

I have been trying to figure out why yuicompressor-maven-plugin is not executed during "mvn package". I can execute it in an independent task as described in the link below but somehow the plugin does not get called from maven life cycle.
http://davidb.github.com/yuicompressor-maven-plugin/usage_compress.html
and here is the sample pom.xml I use,
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- yuicompressor-maven-plugin -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<id>compress</id>
<phase>process-resources</phase>
<goals>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<linebreakpos>-1</linebreakpos>
<encoding>UTF-8</encoding>
<nosuffix>true</nosuffix>
<force>true</force>
<jswarn>false</jswarn>
<webappDirectory>${project.build.directory}/minified</webappDirectory>
<aggregations>
<aggregation>
<insertNewLine>true</insertNewLine>
<output>${project.build.directory}/${project.build.finalName}/js/abc-min.js</output>
<includes>
<include>${basedir}/src/main/webapp/js/comments.txt</include>
<include>${project.build.directory}/minified/js/def.js</include>
</includes>
</aggregation>
</aggregations>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warSourceExcludes>js/**/*.js,js/**/*.txt,css/**/*.css,css/**/*.txt</warSourceExcludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
I have read similar posts but could not figure out why it's not bounded to "process-resources". Phase and Goal are explicitly set so not sure why it's not called. Is there a way to debug why yuicompressor-maven-plugin is not called during "mvn package"? I use maven 2.2.1. Perhaps the version of my Maven won't work with the plugin?
thanks for your help,
syamashi
You simply need to put the running out of the pluginManagement area into the usual plugins area like:
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>1.3.0</version>
....
</plugin>
</plugins>
</build>
In pluginManagement you define only the default for plugin in particular the verison but you really don't bound to real execution. For other plugins etc. this works cause the are alreay defined in the build area of the supoer pom so this give you the opportunity to redefine them via pluginManagement but not with a plugin which never has been part of any build area.

How can you package a GWT module as a Jar file with Maven?

I need to setup Maven to:
a) compile the GWT module
b) copy the *.java files in the jar (so it can be imported in another GWT module)
c) copy the result of the compilation step in the jar (so it can be used on a server as is)
Does any one know how this can be done ?
The basic idea is that I want to decouple my GWT project from my Spring MVC project and remove any dependencies that the Spring application might have to GWT jars & plug-ins.
That way I can use the GWT modules as pure javascript libraries and load them with org.springframework.js.resource.ResourceServlet directly from the Jar files while still maintaining the flexibility to re-use modules in other GWT projects.
Any help would be appreciated.
I'm attaching the solution I came up with so others can use it:
<!-- Set the output directory to something gwt:run can use in hosted mode -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<finalName>gwt-build-name</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>${gwt.version}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<hostedWebapp>${project.build.directory}/${project.build.finalName}</hostedWebapp>
</configuration>
</plugin>
<!-- Attach the resources plugin to the prepare-package phase to include the host page & generated javascript files -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>package-generated-javascript</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- shove everything the compiler produced into the JAR/META-INF/ folder so that Spring resourceServlet can find it -->
<outputDirectory>${project.build.outputDirectory}/META-INF</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/${project.build.finalName}</directory>
<includes>
<include>org.yournamehere.Main/**</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>include-host-page</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/webapp</directory>
<includes>
<include>**</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
What the above does is change the output directory to target/finalName so that everything ends up under the same directory and attach the resources plugin to the compile, prepare-package phases to copy the GWT compiler output to the build directory. Once everything is there it will end up in the final jar by default.
This way the build directory contains everything that hosted mode needs to run and everything Spring resources servlet needs to serve the GWT module without any direct dependencies to GWT.

Maven, filtering NON-resource file

I share a config file between several modules and I don't want the config file to be baked into any of the JARs.
How can i make Maven do (resource) filtering on the file which is not specified as a resource but is in a config folder on the same level as the root POM?
You could use the Maven Resources Plugin and its resources:copy-resources mojo. From the Examples:
Copy Resources
You can use the mojo copy-resources to
copy resources which are not in the
default maven layout or not declared
in the build/resources element and
attach it to a phase
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
Another option would be to use the Maven AntRun Plugin and Ant filtering capabilities (e.g. with the Filter and/or the Copy tasks) but the above looks just fine.

Maven - Add directory to classpath while executing tests

The Junits I have in my project need to load property files from the classpath. How can I specify the directory of those property files so that Maven will set that in the classpath before running the tests?
You can also add new test resource folders.
<build>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
<testResource>
<directory>${project.basedir}/src/test/something_else</directory>
</testResource>
</testResources>
</build>
The first path, src/test/resources, is the default. Assuming you still want the default path to be used, make sure it's included. (The testResources tag overwrites your defaults, so if you don't include the default path explicitly, it will stop being used.)
You can use the build-helper-maven-plugin to specify additional test-resource directories as follows. Using the configuration below, the contents of the test-resources directory will be copied to the target/test-classes directory during the generate-test-sources phase:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>add-test-resource</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>path/to/additional/test/resources</directory>
<excludes>
<exclude>**/folder-to-exclude/**</exclude>
</excludes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
If you just want to put your property files someplace on disk and don't want to copy those property files to target/test-classes during the build, you can do it this way
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>/add/this/to/path</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
Why not just use test/resources and place your properties in the classpath from that point. They'll only be there for the test phase.
If you have multiple resource environment you can use maven profile and put your various resources according to the profile you are testing.
test/resources/uat
test/resources/prod
test/resources/dev
But usualy if you need that you are making integration test then you don't need the build-helper-maven-plugin.
The maven-resources-plugin has a copy-resources goal that will allow you to copy resources. For example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>additional-resources</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/conf</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This will copy the contents of the conf folder in the base of your project to the target/test-classes folder (unless you modified project.build.testOutputDirectory) which will be added to the classpath during your unit tests.