简体   繁体   中英

How to run tomcat7-maven-plugin, that server code changes will be updated immediately

I have project in maven. Here is main pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>pl.derp</groupId>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <modules>
        <module>server</module>
        <module>shared</module>
        <module>web</module>
    </modules>

    <properties>
        <gwtVersion>2.7.0</gwtVersion>
        <webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <tomcat.webport>8083</tomcat.webport>
        <tomcat.ajpport>8182</tomcat.ajpport>
        <tomcat.context>/parent</tomcat.context>

        <hibernate.version>4.3.7.Final</hibernate.version>
        <mysql.connector.version>5.1.21</mysql.connector.version>

        <slf4j.version>1.6.4</slf4j.version>
        <spring.version>3.0.5.RELEASE</spring.version>
        <guice.version>3.0</guice.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-servlet</artifactId>
            <version>${gwtVersion}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.google.gwt</groupId>
            <artifactId>gwt-user</artifactId>
            <version>${gwtVersion}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
            <classifier>sources</classifier>
            <scope>test</scope>
        </dependency>
        <!-- DB related dependencies -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.182</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <!-- Custom formats -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.jadira.usertype</groupId>
            <artifactId>usertype.extended</artifactId>
            <version>3.2.0.GA</version>
        </dependency>


        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.version}</version>
        </dependency>


        <!-- Tests -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

    </dependencies>


    <build>

        <defaultGoal>clean install tomcat7:run-war-only</defaultGoal>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>gwt-maven-plugin</artifactId>
                <version>${gwtVersion}</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
            </plugin>

            <!-- IDE -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.7</version>  <!-- Note 2.8 does not work with AspectJ aspect path -->
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                    <wtpversion>2.0</wtpversion>
                    <additionalBuildcommands>
                        <buildCommand>
                            <name>org.eclipse.jdt.core.javabuilder</name>
                            <arguments>
                            </arguments>
                        </buildCommand>
                        <buildCommand>
                            <name>com.google.gdt.eclipse.core.webAppProjectValidator</name>
                            <arguments>
                            </arguments>
                        </buildCommand>
                        <buildCommand>
                            <name>com.google.gwt.eclipse.core.gwtProjectValidator</name>
                            <arguments>
                            </arguments>
                        </buildCommand>
                    </additionalBuildcommands>
                    <additionalProjectnatures>
                        <projectnature>com.google.gwt.eclipse.core.gwtNature</projectnature>
                    </additionalProjectnatures>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <path>${tomcat.context}</path>
                    <port>${tomcat.webport}</port>
                    <ajpPort>${tomcat.ajpport}</ajpPort>
                    <systemProperties>
                        <JAVA_OPTS>-XX:MaxPermSize=256m</JAVA_OPTS>
                    </systemProperties>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I am using tomcat7-maven-plugin as You can see in pom.xml.

To start server I use command: maven clean install tomcat7:run-war-only

But unfortunately when I do any change in server side/tier code I have to stop server and run it again, which takes a lot of time, cause of: test runs, full compilation, all permutation compilation (it is gwt app, so it compiles java code to js code for each web browser)...

How can I set up auto code replace/hot swap on runtime, that I don't have to restart and recompile my project, when I do one little change?

I would speed up my developing process.

Please give me some help.

I guess the bit which takes the longest is the GWT complication. If you are doing all your development with a single browser, you can tell GWT to only compile the permutation for that browser. See this question and accepted answer to see how. That should cut down your build time.

Also, you can tell Maven to skip tests. This is not really a practice I'd recommend, but if you are really sure your change is trivial, or does not involve Java code, then use the Maven switch -DskipTests

If you change only involves resources such as HTML, CSS, JS, JSP etc then you can just copy them directly to $tomcat_home/webapps/yourapp and Tomcat will pick them up, no build needed. Take care to always have the newest version in your source code.

Finally, the Tomcat plugin should not require a Tomcat restart. Try

mvn tomcat7:redeploy

并非为此目的而设计tomcat7-maven-plugin,请使用IDE(IntelliJ,Eclipse,Netbeans等)或动态类加载器(如JRebel)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM