Only copy modified files in maven-war-plugin - plugins

I'm currently using maven 3.x with the maven-war-plugin. For developer builds I would like to be able to use the war:exploaded goal, but only copy resources that have changed. Is there a way to do this?
I've been looking through the docs and have not been able to find a way to do this in the current versions, but there used to be (in the old maven 1.x version) a property maven.war.resources.overwrite that would allow this.
Thanks.

I'm not aware of a way to do this using the maven-war-plugin, but if your IDE supports it, you could have the IDE auto-deploy changes. For example, the Web Tools Platform for Eclipse supports this feature for Tomcat. However, if your build process is complex or does something weird before invoking the maven-war-plugin, this may not work for you.
A second option: if you're running Linux, set up rsync to copy recently modified files to your servlet container. A co-worker did this by having the servlet container's web app directory sync with the Eclipse project's output directory, and it worked well (just modify your code, Eclipse will build it automatically, and rsync will copy your changes).

For this purpose I use maven-antrun-plugin
Example:
<project xmlns="http://maven.apache.org/POM/4.0.0"
....
....
<!--DJ: Settings for deploy only-->
<!--DJ: Dir to where copy files-->
<!--DJ: And date when build have been started, to select only modified files in the future-->
<properties>
<tomcat.dir.rootDir>C:\apache-tomcat-6.0.35\webapps\ROOT1</tomcat.dir.rootDir>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>
.....
<!--Ohter dependensies here-->
.....
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copyModifiedFilesFrom_ExplodedWAR_Dir</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Upload new files to dir ${tomcat.dir.rootDir} modified after date ${maven.build.timestamp} "/>
<copy todir="${tomcat.dir.rootDir}">
<fileset dir="${project.build.directory}/${project.build.finalName}" includes="**/*">
<!-- Include only recompiled files -->
<date datetime="${maven.build.timestamp}" pattern="yyyy-MM-dd HH:mm:ss" when="after"/>
<!-- and only *.class files -->
<filename name="**/*.class"/>
</fileset>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
.....
.....
</plugins>
</build>
</project>
Then run maven with params:
mvn pom.xml compile install org.apache.maven.plugins:maven-war-plugin:2.1-alpha-1:exploded
As result only changed files will be recompiled and only recompiled filed will be replaced in tomcat webapp dir.

Related

How to change jar output directory when running as maven install in Eclipse?

I am making a Minecraft plugin for Bukkit 1.8, and everything works fine. I right click on the project name > Run As > Maven install. It outputs the .jar file to the target directory. I then copy the file to the plugins folder of my Minecraft server.
I would like to have it output the jar directly into my plugins folder.
A simple way to do that would be to bind an execution of the maven-antrun-plugin to the install phase. This execution would copy the main artifact to the Minecraft server folder.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${project.build.finalName}.jar"
todir="/path/to/server/plugins" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
(This snippet must be placed inside the <build><plugins> element).
Running mvn clean install (or "Run As... > Maven Install" in Eclipse), Maven will do what you want. ${project.build.directory}/${project.build.finalName}.jar refers to the main artifact present in the build directory (which is target by default). You'll need to update the path to the server in the snippet above.

How to launch browser automatically after war deployment?

I have a war maven project and I want that when I deploy it on the server the default browser (Mozilla in my case) is launched automatically with the default url for access to the main page.
Of course with JBoss EAP6 I have just to run the Maven command clean install jboss-as:deploy to generate the war file and deploy it on the server.
Do I have to add something to the pom.xml or make any configurations in Eclipse?
Use maven's exec plug-in. More information here.
I used this and it works good but juste for the maven phase : install but for the deploy no:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>Run URL in system browser.</id>
<phase>install</phase>
<configuration>
<target>
<exec executable="start" vmlauncher="false">
<arg line="http://localhost:8080/mywarApp" />
</exec>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
I use eclipse and i made for goals "clean install jboss-as:deploy" into the run configurations. and when in click in , once the install phase is runing the browser is runing too but i want that it will happen when the deploy phase is end of runing , have you any idea

Eclipse javadoc: The type package-info is already defined

OS: Windows 7 x64
Eclipse Platform: 3.7.2.M20120208
m2e: 1.0.200.20111228-1245
Have similar problem as in this bug.
There is a bunch of package-info.java files in /src and /test folders, so they have same package. Eclipse show error:
"The type **package-info** is already defined"
I can delete package-info.java files either in /test or /src to avoid problem indication. But this workaround is not very comfort since I am using SCM and need to delete this files all time after update.
Same for Eclipse Platform 4.2.0.I20120608-1400
You can do this -->
Go to Build path -> configure build path -->
in Source tab -->
select the package (in which you have these problematic package-info.java file)
for eg. project-name/src/test/java
click on exclude -> and in exclusion pattern add "**/package-info.java"
this should solve the problem, as plainly you are asking eclipse to exclude these files, and thus you wouldn't have to delete those files and solving your SCM related issues
There are a few options to solve this:
Move away from package-info.java files, and replace them with package.html files.
Only have package-info.java files in the src/ tree, as the same-named packages in the test/ tree will "overlap" the src/ tree.
Generate javadoc separately for the src/ and test/ trees, as they are probably for different audiences.
If you use maven and m2e for interaction between eclipse and maven. There is a quite clean solution: add a profile to pom.xml that is activated only by m2e and prevents compilation of package-info.java in the test-compile phase. Here a sample :
<profile>
<id>m2e</id><!--This profile is activated when eclipse interacts with maven (using m2e).-->
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<!--eclipse do not support duplicated package-info.java, in both src and test.-->
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<configuration>
<testExcludes>
<exclude>**/package-info.java</exclude>
</testExcludes>
</configuration>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

maven eclipse checkstyle plugin

I have custom checkstyle checks file (called checks.xml), and I'm trying to use that same file in both maven and eclipse. It all works well, except for the SuppressionFilter.
In this checks.xml file, I have
<module name="SuppressionFilter">
<property name="file" value="src/main/resources/checkstyle/checkstyle-suppressions.xml"/>
</module>
This works when I run through maven. However, when I run through eclipse, I need to change the config to be
<module name="SuppressionFilter">
<property name="file" value="${basedir}/src/main/resources/checkstyle/checkstyle-suppressions.xml"/>
</module>
If I run with the ${basedir} property with maven though, I get the error that property ${basedir} has not been set.
Is there a way use this same configuration file in both maven and eclipse? I feel like there should be, but I'm just missing something on how to properly populate the suppression filter.
thanks,
Jeff
This is hell. Eclipse and Maven handle suppressions different and don't share variables.
Derived from Rolf Engelhard
So in pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.8</version>
<configuration>
<propertyExpansion>config_loc=${basedir}/src/main/checkstyle</propertyExpansion>
<configLocation>${basedir}/src/main/checkstyle/checkstyle.xml</configLocation>
<suppressionsLocation>${basedir}/src/main/checkstyle/suppressions.xml</suppressionsLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
Now in checkstyle.xml (${config_log} is an Eclipse specific thing, but by specifying it in the pom we make it available to maven as well):
<module name="SuppressionFilter">
<property name="file" value="${config_loc}/suppressions.xml" />
</module>
And if you're using maven-site-plugin or any other plugins that also rely on CheckStyle don't forget to update those to have the config_loc property as well (or declare it global to the pom, though I wasn't able to get this to work properly).
Sure there is a way to use the same configuration file in both maven and eclipse but it requires a little setup first. I wrote a blog post on how to achieve this even for a multi-module maven project. see: maven-checkstyle-and-eclipse
<propertyExpansion>basedir=${session.executionRootDirectory}</propertyExpansion> works for me, but only when added to the <plugin>node, not to <execution>!
project.basedir does not work well in multi-module projects, because it will point to the submodule folder instead of the root folder.
You could try defining ${basedir} as a property in your pom.
See the pom reference quick overview.
<property>
<name>basedir</name>
<value>${project.basedir}</value>
</property>

How to debug an OSGi Bundle inside eclipse using maven structured project

I have a maven project which library should also be an OSGi bundle with an
declarative service. I added the OSGI-INF folder with the service.xml inside
src/java/resources which will be added to the jar. But: When I start the project as equinox project, where I want to check if the service is loaded, I get the error that the OSGI-INF/service.xml can't be found. I guess eclipse won't add the resources folder to the classpath when starting.
BTW: The MANIFEST-MF is in the root folder and the pom.xml contains the following text:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
Has anbody a good idea how to tell eclipse where to find the metadata files? I'm using m2eclipse if this is relevant.
Thanks in advance
Hannes
I've the same issues but I've manually tweaked my POM.xml for copying the generate target/classes/META-INF/** stuff (MANIFEST.MF, property files, spring XMLs, ...) into the project ROOT folder (which Eclipse PDE expects):
<!--
We copy all stuff from target/classes/META-INF into META-INF/ in order
to keep Maven output with PDE.
-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>synch-pde-metadata-from-maven</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/META-INF</outputDirectory>
<resources>
<resource>
<directory>target/classes/META-INF</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--
We delete all stuff from the root bundle's META-INF
-->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<filesets>
<fileset>
<directory>META-INF</directory>
<includes>
<include>**/*</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
(I also clean up things when mvn clean is called) It is not really efficient but it works.
Ps: you may have to right-click on the project and make it a plug-in project in order for this to work.
The Problem is that Eclipse needs that OSGI-INF (and also META-INF) Folder in root.
I have found a link on how to probably solve the problem.
https://www.nuxeo.com/blog/working-with-osgi-and-maven-in-eclipse/
Basically they put the OSGI-INF and META-INF Folders into src/main/resources
Then they set the src/main/resources Folder as root of the OSGI project. So that Eclipse has both Folders at root.
To have Soure Files also available they added a Linked Resources to src/main/java by adding an entry to the .project File
<linkedResources>
<link>
<name>src</name>
<type>2</type>
<locationURI>PARENT-1-PROJECT_LOC/java</locationURI>
</link>
</linkedResources>
Now you just need to copy the .prject and .classpath Files into src/main/resources (your new root) and everything should be working.
By the time of this writing i didn't test this on my own but will do so in the near future.