Do I have to build maven webapp project everytime i make changes into static files? - spring

I am using JBOSS AS7.1, Eclipse Luna for development. My eclipse installation does have a plugin installed for maven.
I have created my webapp project using maven command-line.
In my current set up, I have to build my maven project using mvn clean install every time for all changes, even for the static files like HTML, CSS.
Then, I have to deploy the generated WAR file using JBOSS console running at http://localhost:9990/console.
I am quite sure that there must be another way to do this. Surely, it does take a hell lot of time.
Please guide me to the approaches I can adopt for faster development.

One option is jrebel. It's not free though.
If you are not bound to JBOSS, you could use spring boot. It also supports automatic restart (spring boot devtools)

You can replace the static file in your target folder and launch a build skipping the compile phase.
It will save you a lot of time, when only updating static files.
It is not a good practice, but should let you achieve your goal.
How to:
Use the maven-clean-plugin to remove the files to replace from the target folder (or they will not be overwritten);
Use the resources tag if your static files are not the only content of the resources folder you want to copy (or your static files are not in resources folder at all);
Use the maven-compiler-plugin to skip the compile phase.
Customize this profile (and use it with mvn clean install -P skip-compile):
<profile>
<id>skip-compile</id>
<build>
<resources> <!-- optional -->
<resource>
<directory>src/main/resources/META-INF</directory>
<targetPath>META-INF</targetPath>
<excludes>
<exclude>**/*.xml</exclude>
</excludes>
<includes>
<include>**/*.html</include>
<include>**/*.css</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>${project.build.outputDirectory}/META-INF</directory>
<excludes>
<exclude>**/not_to_delete.xml</exclude>
</excludes>
<includes>
<include>**/*.html</include>
<include>**/*.css</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<skipMain>true</skipMain>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

Related

How to Add JUnit Results to Tycho Built JAR

We are using Maven and sometimes Tycho, and I want to put the results of the JUnit tests into the resulting JARs.
With Maven, that's easy. I just added the following to the pom.xml:
<resources>
<resource>
<directory>target/</directory>
<includes>
<include>surefire-reports/*.*</include>
</includes>
</resource>
</resources>
The test phase is before the package phase, so all is well.
For Tycho however, tests are executed after the package phase (because they are executed in the verify phase for some reason). The phase cannot be changed either (see bug 440094).
So the only alternative is to build another JAR after the verify phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>post-integration-test</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>verification-doc</classifier>
<includes>
<include>**/surefire-reports/*.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
However, it does not work. Neither the folder nor the files are added. My guess was that it's because of the include tag points to the source folders, so I tried:
<include>${project.build.outputDirectory}/../surefire-reports/*.*</include>
<include>${project.build.directory}/surefire-reports/*.*</include>
Which did not work either?
So how do I add JUnit test results to the JAR of a Tycho build?
You can change the directory of the maven-bundle-plugin like this:
<configuration>
<classesDirectory>${project.build.directory}</classesDirectory>
<includes>
<include>surefire-reports/*</include>
</includes>
</configuration>

How to replace a complete property file with another while maven build

I am having email.properties(development) in my application under resources folder. This file has to be overwrite another file (email.properties for production) during maven build. Could you please suggest steps to achieve this. I have gone through some process to change values with in property file by using tokens. But that is not my actual requirement, if I keep tokens like mail.id={your.mail} i am unable to use it in my local host because i am not doing any maven build and deploying war file in my JBOSS for localhost.
You can use the maven resources plugin to overwrite the resource with another one
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-properties</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources/prod</directory>
<includes>
<include>environment.properties</include>
</includes>
</resource>
</resources>
<overwrite>true</overwrite>
</configuration>
</execution>
</executions>
</plugin>
You can bind it to a profile in case you don't always need an overwrite.

Maven war packaging, include classes ignores excluded resources

I have the following pom
<project>
....
<packaging>war</packaging>
....
<build>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archiveClasses>true</archiveClasses>
<warSourceExcludes>WEB-INF/sass/**</warSourceExcludes>
<attachClasses>true</attachClasses>
<webResources>
<resource>
<directory>src/main/resources/config</directory>
<excludes>
<exclude>**/*</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
...
</project>
As you can see, I package a WAR while my .class files are not in my WEB-INF/classes folder - they are packaged into a JAR instead.
Now, I am desperately trying to exclude some resources from my JAR - but it does not work. When I run mvn jar:jar - the resources are excluded, however when I run mvn package the resources are there.
Please help.
It seems that #user944849 is right - indeed, the war plugin does not use the jar plugin in order to achieve the JAR packaging.
However, his answer gave me a wrong result still as it will simply create 2 jars - one will be with the resources and the other without. The WAR will still use the wrong one.
The correct answer is to use the new maven resources tag.
The one that corresponds to my configuration looks as follows
<build>
....
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
....
</build>
You do not have the jar:jar goal bound to a lifecycle phase. When you run jar:jar from the command line, the exclusions happen fine; when you run mvn clean package I suspect jar:jar is not executing. Try binding the goal to a phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals><goal>jar</goal></goals>
<phase>prepare-package</phase>
<configuration>
<excludes>
<exclude>config/**/*</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
FYI, the archiveClasses feature of the war:war plugin goal does something similar to what I think you're trying to achieve without requiring a separate plugin config.

Rename static files on maven war build

I am trying to figure out a way by which I could rename the static files I have in my web project, all of them at the same time.
Exactly what I need is to be able of filtering all my statics filenames to add a version number or something similar just to avoid them from being cached by browsers.
Something like converting custom.css into custom-1.23.css where 1.23 would be the value of a given filter.
This behaviour looks really similar to what resources plugin does with the content of the files, but I couldn't find a way of doing the same with the filenames.
Does anyone knows something similar?
Thanks a lot
You could change the directory they are served from rather than the file name e.g.
/static/${version}/custom.css
The resources plugin would let you change the target directory in the war.
1) Use any Maven plugin that is able to copy and rename resources. For example, copy-rename-maven-plugin. Configure the plugin to copy all static resources which you want to version and put them into a new directory inside the target directory, for example target/static_versioned:
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>copy-file</id>
<phase>generate-sources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<sourceFile>src/main/webapp/css/style.css</sourceFile>
<destinationFile>target/static_versioned/css/style_${project.version}.css</destinationFile>
</fileSet>
<fileSet>
<sourceFile>src/main/webapp/js/app.js</sourceFile>
<destinationFile>target/static_versioned/js/app_${project.version}.js</destinationFile>
</fileSet>
</fileSets>
</configuration>
</execution>
</executions>
</plugin>
2) Configure maven-war-plugin to add versioned static files inside you war file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webResources>
<resource>
<directory>target/static_versioned</directory>
</resource>
</webResources>
</configuration>
</plugin>
3) Maven copies both versioned and original files into the result war file, so the original files need to be excluded:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<packagingExcludes>**/css/style.css,**/js/app.js</packagingExcludes>
<webResources>
<resource>
<directory>target/static_versioned</directory>
</resource>
</webResources>
</configuration>
</plugin>
It is worth mentioning that the target directory still has original and versioned files, but the war file contains only versioned files.
4) Use maven-war-plugin's filtering functionality to rename static resource links so all links point to versioned resources:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<packagingExcludes>**/css/style.css,**/js/app.js</packagingExcludes>
<webResources>
<resource>
<directory>target/static_versioned</directory>
</resource>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
For example, a link like <script type="text/javascript" src="js/app_${project.version}.js"></script> will be renamed into <script type="text/javascript" src="js/app_1.0-SNAPSHOT.js"></script>
Complete example is here: https://github.com/dmitrysobolev/maven-war-plugin-js-versioning-example

delete the 'target' directory after build

i know this is probably frowned upon by maven lovers, but the whole 'target' directory is a waste of space in the context of our program and it's deployment. we have other build processes responsible for creating the actual deployment and i currently manually delete the target dir after every maven build, so that its contents don't interfere with my file searches etc...
is there a way to delete this dir automatically at the end of a maven build/install?
thanks, p.
Use the maven-clean-plugin as here http://maven.apache.org/plugins/maven-clean-plugin/examples/delete_additional_files.html
<project>
[...]
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>install</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
You should simply add the clean goal to your maven goals at the end.
mvn install clean
The problem with the clean-plugin is that if you like to run the clean at the end of the build it depends which goal you called at the beginning. For example you called mvn package you need to have a phase post-package which does not exist or if you called mvn install you have to have phase post-install which does not exist either.
The easiest way is simply to add "clean" to the end of the normal build command. eg. mvn clean install clean.
if you want to just delete some directories in target folder, you have to create some construct like this.
this for instance deletes just all contents of folders:
target/unpack
gen-external-apklibs
excludeDefaultDirectories allows to not delete complete target folder.
i used it to clean up target folder before lint analysis.
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>Deleting all unnecessary files before lint analysis</id>
<phase>verify</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>target/unpack</directory>
<followSymlinks>false</followSymlinks>
<excludes>
<exclude>*</exclude>
</excludes>
</fileset>
<fileset>
<directory>gen-external-apklibs</directory>
<followSymlinks>false</followSymlinks>
<excludes>
<exclude>*</exclude>
</excludes>
</fileset>
</filesets>
<verbose>true</verbose>
</configuration>
</plugin>

Resources