How to hot deploy JSP changes in maven app? - maven

I have a maven web application. I am using springsource tool suite and its built in tc fabric server.
Every time I make any changes I have to do mvn clean install and restart the server. Even for the JSP changes.
Is there any way I can just make JSP changes and they are reflected in the browser on refresh like a regular web app (not maven app).
I have searched over internet but no success as yet.
Still No clue about it. It makes the development process extremely slow. I looked into jrebel but its not free and I am not looking for hot deploy of classes but just the hot deploy of JSP's, javascripts, css etc.

I currently use a profile to copy JSPs to my target dir, which I call from Eclipse once i need the JSPs updated.
You could also copy classfiles like this by adding executions.
<profile>
<id>copyJsps</id>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<outputDirectory>${basedir}/target/app/WEB-INF/jsp</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp/WEB-INF/jsp</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Use: mvn resources:copy-resources -PcopyJsps

You can use Eclipse Filesync plugin to achieve this. You can configure plugin to map maven output folder to Application server directory
I know this is not maven way but it works.
Maybe this question will give some more insights.

This is my simple solution (workaround?) while using JBoss on Linux. It should also be valid for Tomcat or any other container that supports exploaded war's.
1. Use mvn war:inplace to create an exploaded war in src/main/webapp.
This will copy classes and lib there. You don't need to repeat this step if you're just changing JSP (or other /webapp/ files).
Alternatively you can create symlinks to classes and lib in src/main/webapp/WEB-INF:
cd src/main/webapp/WEB-INF
ln -s ../../../../target/classes
mvn package
ln -s ../../../../target/*/WEB-INF/lib
2. In the deployment directory create a symlink to src/main/webapp:
cd $DEPLOYMEN_DIR
ln -s $MYAPP_DIR/src/main/webapp myapp.war
This makes changes to JSP and other webapp files instantly available.
If you want to see the changes in your classes then you can trigger a reload of the application only my modifying web.xml. On you can run this script to monitor trigger the restart:
#!/bin/bash
while sleep 1; do
if [[ -n $(find WEB-INF/classes -newer WEB-INF/web.xml -type f) ]];then
date
touch WEB-INF/web.xml
fi
done
Run it from the src/main/webapp/ directory.
Note on JBoss AS 7: Here to trigger a reload you need to create a myapp.war.dodeploy file instead of touching web.xml

You can clean and copy the static files easy with the clean and resources plugins, but it won't work for the JSP files always. If the java source in the JSP files being copied introduces a new dependency you are not going to copy it to the lib folder. In that case, the app will break with a ClassNotFoundException.
Even if it is copied it can still break because the server has to be configured to scan the folder with dependencies and refresh the classpath. And that is the start of hot deployment I believe (details).
Try with Vinay's suggestion also. Judging by his answer it seems that the tc server supports scanning of dependencies by default, and with proper maven build this might be a satisfying solution.
To clean and copy the static files from your source directory to where it is deployed:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>clean-loaded</id>
<phase>compile</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>${path.server.input}</directory>
<followSymlinks>false</followSymlinks>
<useDefaultExcludes>false</useDefaultExcludes>
<includes>
<include>**/*.jsp</include>
<include>**/*.js</include>
<include>**/*.html</include>
<include>**/*.css</include>
<include>**/*.png</include>
<include>**/*.gif</include>
<include>**/*.jpg</include>
<include>**/*.jpeg</include>
</includes>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-compile-output</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${path.server.input}</outputDirectory>
<overwrite>true</overwrite>
<includeEmptyDirs>true</includeEmptyDirs>
<filtering>true</filtering>
<resources>
<resource>
<directory>${path.jsp.source}</directory>
<targetPath>${path.element.jsp.deploy}</targetPath>
<includes>
<include>**/*.jsp</include>
</includes>
</resource>
<resource>
<directory>${path.static.source}</directory>
<targetPath>${path.element.static.deploy}</targetPath>
<includes>
<include>**/*.js</include>
<include>**/*.html</include>
<include>**/*.css</include>
<include>**/*.png</include>
<include>**/*.gif</include>
<include>**/*.jpg</include>
<include>**/*.jpeg</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
add this to your properties section:
<path.server.input>ABSOLUTE_PATH_TO_DEPLOYED_WEBAPP_ROOT</path.server.input>
<path.jsp.source>ABSOLUTE_PATH_TO_JSP_SOURCE_ROOT</path.jsp.source>
<path.static.source>ABSOLUTE_PATH_TO_STATIC_SOURCE_ROOT</path.static.source>
<path.element.jsp.deploy>REALTIVE_PATH_TO_JSP_DEPLOY_ROOT</path.element.jsp.deploy>
<path.element.static.deploy>REALTIVE_PATH_TO_STATIC_DEPLOY_ROOT</path.element.static.deploy>
Properties that start with path must be absolute paths or start with a ${project.basedir} or similar. Properties that start with path.element are relative paths meaning that they must not be prepended with a / or start with another property that is an absoulte path. That is so because resources plugin copies in outputDirectory/targetPath (resources:copy-resources,
resource)
In my experience, IDE's usually bind their clean-and-build UI action to the compile phase. Also IDE's usually have a way to map a shell command or maven custom goal to be visible from it's UI menu.
To run with clean-and-build
The plugins are already bound to the compile phase. To ensure that clean plugin will run before resources plugin at the end of the compile phase, place them together at the end of your plugins section. It doesn't matter if a plugin is defined twice, just make sure that when reading the pom from top, the first clean plugin definition comes before the first resources plugin definition.
To run as separate action
Change under the execution tag for both like this:
<id>default-cli</id>
<phase>never</phase>
and now it won't be run in compile phase but by invoking from command line:
mvn clean:clean resources:copy-resources
In this case the placement of plugin definitions in pom is irrelevant, since you are defining their order with command arguments order. If this suits you your IDE most probably has a way to map this command as a custom goal visible from it's UI menu.
In both cases, I recommend backing-up the project folder when running for the first time.

Related

Sencha cmd build : specify 2 --destinations

I am calling the following as part of a Maven build to generate my ExtJS project resources
sencha app build --destination "some path"
There is a requirement to copy the generated resource files into 2 locations within the project. Ideally I could specify 2 paths like this
sencha app build --destination "first path" "second path"
This is not working nor is specifying the --destination argument before each path.
I could use a symlink to copy contents of first path into second but this could get complicated when working on branches where the symlink is not defined.
I looked into the Maven resources plugin: copy-resources but it starts copying the files before the Sencha build has finished. How can I make this plugin wait until the build has finished ?
Any other suggestions welcome
I managed to achieve this by changing the phase element value to 'package'. It now waits until the sencha build is complete before copying resources
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-appCtx</id>
**<phase>package</phase>**
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/src/</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${project.basedir}/othersrc/</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

ant to maven conversion: correct approach?

I am tasked with converting a java project which is created with ant to maven.
This is how the project is set up.
All the sources are stored in src directory.
ant's compiling target is to compile the entire src directory.
ant's packaging target has several sub-targets.
Each target has different jars which has include or exclude directories.
This is the approach that I took.
Find out all dependencies. Store them in DependencyManagement section of parent pom
Create a module and copy entire src directory.
compiled it.
Tried to create separate modules for different jar files.
Problem: the files are in-separable. Most of the files are depending on other files. I tried separating them. It results in creating cyclic dependencies. Hence, this step failed.
Use different profiles and maven-jar-plugin to include or exclude packages.
Question 1 when I tried this mvn install -P profile1,profile2, target has only jar file for profile2. They both have maven-jar-plugin and each has different finalName.
<profile>
<id>profile1</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<includes>
<include>**/src/.../profile1/**</include>
<finalName>profile1-lib</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Is there a different command or am I doing something wrong?
Question 2 one of the lib has several image files in it.
The above approach does not copy the image files in the result jar.
I understand maven wants all resources in resources directory. I will move the images, but for now I am trying to include them in the jar.
I added maven-resources-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resource>
<directory>src/main/java/jpl/mipl/mdms/FileService/komodo/ui/savannah/subscription/util/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
When I run mvn install -P profile1, I can see the logs saying Copying # resources.
I can also see them being copied to target/classes. the path that I gave to the plug-in. But they are all copied to that directory.
I want to retain the structure, and the result jar still doesn't have it.
Answer: I have to move them to resources directory. maven is strict.
Anything that I should be doing differently?
Extra Question Am I using the correct plug-ins? Or is there more efficient plug-ins that I should be using?
Thank you.
Answer to Question1 : You have missed the </includes> tag.(Probably because of the stackoverflow formatting!). I am not sure whether you can execute a maven command on two profiles at a time. when you do so, only the second profile gets executed. Try executing the command on each profile separately.
Answer to Question2: You have missed the <resources> opening tag.(Probably because of the stackoverflow formatting!). Moreover, if you want to retain the structure, you can mention the structure too in the <outputDirectory> tag, something like the below:
<outputDirectory>${basedir}/target/classes/src/main/java/jpl/mipl/mdms/FileService/komodo/ui/savannah/subscription/util/resources</outputDirectory>
This may look insane!, but may work if you have few resources directory. If you have more resources, then this may become cumbersome. But anyways, check whether this can be helpful!

Maven 2.2.1: [Warning] JAR will be empty - no content was marked for inclusion

Maven 2.2.1
JDK - 1.6.0_45
[WARNING] JAR will be empty - no content was marked for inclusion!
BUILD SUCCESSFUL
But build creates jar with pom.xml but no class files.
On the maven source code this exception is thrown only when source directory is not found.
The build is working for all other developers except on my workstation and one more workstation
I have tried all the solutions provided for this issue on stack overflow.
My source directory is src/java.
I also created src/main/java as source still no result.
I am calling mvn -o jar:jar && call mvn -o antrun:run
-o is becuase at this point I am testing with some old jars.
<build>
<sourceDirectory>src/java</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>${basedir}/src/test/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<debug>true</debug>
<optimize>false</optimize>
<showDeprecation>true</showDeprecation>
<source>1.5</source>
<target>1.5 </target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>test*/temp/*.java</exclude>
<exclude>test*/support/*.java</exclude>
<exclude>test*/debug/*.java</exclude>
</excludes>
<includes>
<include>test*/**/AllTests.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>default-cli</id>
<phase>install</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${artifactId}-${version}.jar"
todir="${user.home}/.m2/repository/${groupId}/${artifactId}/${version}" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
First follow the conventions in Maven which means your production sources code should be in src/main/java. You should also locate your resources like property files or other kind of files (like xml files) in your case to the proper location which is for production src/main/resources and for unit tests src/test/resources.
The first thing you should change is the directory structure for your project in the process in migration. That will save many hassles with configurations in Maven cause you are violating the convention over configuration paradigm.
Your unit tests code in src/test/java and follow the naming conventions for unit tests which means name your unit tests like *Test.java nothing more. You don't need to define a suite to run all the tests. If you follow the naming convention maven-surefire-plugin will do all the work for you.
Remove the antrun plugin from your pom configuration and use
mvn install
instead to install your produced jar into local repository. Based on the build life cycle you will compile, unit test and package your code into resulting jar files.
Usually in Maven there is no reason to call mvn jar:jar separately.
Apart from that all you should stop using Maven 2.2.1 cause it has defined End Of Life. Better start with Maven 3.X instead. But everything i wrote before is valid Maven 3.
I got Build Success but same error:
JAR will be empty - no content was marked for inclusion.
It was a test project and I realized that I had no "main" under "src". As soon as I corrected this, it was fixed. I am adding the wrong and right structure screenshots in the attachments:
right structure
wrong structure - missing main folder

Configure maven-shade-plugin to include src/main/webapp

I'm working with a simple project, with the webapp directory in the standard location (src/main/webapp). For some reason, it looks like the maven-shade-plugin doesn't include that in the final generated jar - it will only include artifacts src/main/resources by default.
I have tried to use the following configuration, with no luck:
<configuration>
<artifactSet>
<includes>
<include>*</include>
<include>*:*</include>
</includes>
</artifactSet>
</configuration>
Any suggestions on how to do that without having to move src/main/webapp into src/main/resources/webapp?
Thank you.
Eugen.
Have you tried to update your build section with your resource path ?
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
</build>
EDIT
As already said, shade purpose is not to war packages.
You need to put it in src/main/resources/webapp or specify the appropriate location explicitly for it to be caught up properly. Annoying, but it works.
For those asking why this kind of thing makes sense to do, it's a typical setup for a jar with an embedded web server like Jetty.
I use the resource plugin to move the webapp to the classes folder.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/webapp</outputDirectory>
<resources>
<resource>
<directory>src/webapp/dist</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This way I can place the folder with my webapp anywhere, define the destination directory and it is included in my shaded jar.
It sounds you would like to package sources into a package. There exists a maven-source-plugin which will create a jar which contain the source files. Furthermore this is usually done during a release cycle. As already mentioned the maven-share-plugin intention is to package jar files together (their contents; keyword: ueberjar).
I would use the war plugin http://maven.apache.org/plugins/maven-war-plugin/faq.html#attached to create a jar of a webapp project. You have already set <packaging>war</packaging>?
What's the point in adding your webapp resources into a JAR? That's WAR-like.
Change your packaking type to war and it will work.

In Maven how to exclude resources from the generated jar?

When I create an executable jar with dependencies (using this guide), all properties files are packaged into that jar too. How to stop it from happening? Thanks.
UPDATE: I tried to exclude them using the Maven resources plugin, but then my application won't find the properties files when I run it in Eclipse (right click on the module -> Run As -> Java Application)
UPDATE: Thanks for your useful answers. I think I'd better spend time to learn Maven, for now I just choose the simplest solution.
To exclude any file from a jar / target directory you can use the <excludes> tag in your pom.xml file.
In the next example, all files with .properties extension will not be included:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
</build>
By convention, the directory src/main/resources contains the resources that will be used by the application. So Maven will include them in the final JAR.
Thus in your application, you will access them using the getResourceAsStream() method, as the resources are loaded in the classpath.
If you need to have them outside your application, do not store them in src/main/resources as they will be bundled by Maven. Of course, you can exclude them (using the link given by chkal) but it is better to create another directory (for example src/main/external-resources) in order to keep the conventions regarding the src/main/resources directory.
In the latter case, you will have to deliver the resources independently as your JAR file (this can be achieved by using the Assembly plugin). If you need to access them in your Eclipse environment, go to the Properties of your project, then in Java Build Path in Sources tab, add the folder (for example src/main/external-resources). Eclipse will then add this directory in the classpath.
This calls exactly for the using the Maven JAR Plugin
For example, if you want to exclude everything under src/test/resources/ from the final jar, put this:
<build>
<plugins>
<!-- configure JAR build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<excludes>
<exclude>src/test/resources/**</exclude>
</excludes>
</configuration>
</plugin>
...
Files under src/test/resources/ will still be available on class-path, they just won't be in resulting JAR.
Put those properties files in src/test/resources. Files in src/test/resources are available within Eclipse automatically via eclipse:eclipse but will not be included in the packaged JAR by Maven.
Exclude specific pattern of file during creation of maven jar using maven-jar-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.exe</exclude>
<exclude>**/*.java</exclude>
<exclude>**/*.xls</exclude>
</excludes>
</configuration>
</plugin>
Do you mean to property files located in src/main/resources? Then you should exclude them using the maven-resource-plugin. See the following page for details:
http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
Another possibility is to use the Maven Shade Plugin, e.g. to exclude a logging properties file used only locally in your IDE:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin-version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>log4j2.xml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
This will however exclude the files from every artifact, so it might not be feasible in every situation.
When I create an executable jar with dependencies (using this guide), all properties files are packaged into that jar too. How to stop it from happening? Thanks.
Properties files from where? Your main jar? Dependencies?
In the former case, putting resources under src/test/resources as suggested is probably the most straight forward and simplest option.
In the later case, you'll have to create a custom assembly descriptor with special excludes/exclude in the unpackOptions.
here is another solution to exclude all files in resources folder, the final configuration looks like:
<build>
<!-- exclude all files in resources-->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/**</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<!-- other configurations/plugins in the pom.xml-->
</build>
or we can use includes to only package some file or folder.
But this method has a side effect. IDE will also exclude the resource files in target/classes folder. maven-jar-plugin only affect jar file.
I found a better solution to execludes resourses folder using maven-jar-plugin, here we use includes:
<build>
<plugins>
<!-- configure JAR build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</plugin>
...

Resources