How to hot deploy JSP changes in maven app?
Asked Answered
F

4

6

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.

Fount answered 2/6, 2013 at 10:30 Comment(0)
V
3

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.

Varhol answered 27/8, 2013 at 14:54 Comment(0)
T
5

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

Terrier answered 19/11, 2013 at 14:46 Comment(1)
most helpfull and can do exactly what the question asks for. +1Malign
V
3

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.

Varhol answered 27/8, 2013 at 14:54 Comment(0)
E
1

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

Emotionalism answered 23/8, 2013 at 11:4 Comment(0)
D
0

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.

Declass answered 29/8, 2013 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.