Maven compiles and publishes the program in eclipse for debugging in tomcat and replaces the replacement character in the resource file

Goal: Use eclipse, compile and publish the program to tomcat through maven, and support breakpoint debugging and filtering of replacement symbols in resource files.

The principle is to compile and replace the replacement character of the resource file through maven, and then call tomcat to start the embedded (embedded) instance to test the program.

Here is what the pom.xml file looks like

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>bb</groupId>
  <artifactId>cw</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>cw Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>servlet-api</artifactId>
	    <version>2.5</version>
	    <scope>provided</scope>
	</dependency>
  </dependencies>
  <build>
    <finalName>cw</finalName>
    <plugins>
    	<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>utf-8</encoding>
                </configuration>
        </plugin>
    	<plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
        </plugin>
    	<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <path>/cw</path>
                    <port>8080</port>
                    <uriEncoding>UTF-8</uriEncoding>
                </configuration>
	</plugin>
    </plugins>
    <resources>
    	<resource>
            	<directory>src/main/resources</directory>
            	<filtering>true</filtering>
        </resource>
    </resources>
    
  </build>
</project>

 The plugin "maven-compiler-plugin" is used to specify that the compiled version is 1.6. It seems that not specifying it will cause some feature syntaxes of jdk above 5.0 to be unsupported (such as @Override).

Then use the plug-in "tomcat-maven-plugin", and modify the path and port to what you want.

The plug-in maven-resources-plugin is also indispensable, which can be configured as above.

Special attention is that there is a resources tag in the build tag, and then there is a resource in it. The directory specifies the default resource file directory. The key is that filtering is set to true, and maven will replace the resource file when copying it to the target directory. Character replacement, replace the ${abo.fdos} in the evn.properties file above. By default, the replaced content is to use the configuration in the profiles.xml in the pom.xml statistics directory. The configuration file can be seen below. Of course, this file is maven2.x, 3.x does not support. The content of the replacement character can also be specified in the profile tag written in pom.xml, etc. There are many other ways to specify the profile content.

Then you need to configure tomcat in eclipse's windows > preferences > server > Runtime Environment.

When maven compiles (compile), it will not find the javax.servlet.Servlet class, even if tomcat is added to the Libraries in the Java Build Path in eclipse, it is useless. Then we need to configure the dependency servlet-api in pom.xml. Special attention is that the scope needs to be specified as provided. If it is not written, or it is compile or something, when I test, a runtime exception occurs. When I access the servlet, it says I The Servlet class cannot be converted to javax.servlet.Servlet. provided may be used at compile time, but when publishing, Servlet-api.jar is not published, but the servlet-api.jar of the container (tomcat) itself is used.

 Added remarks: After later testing, in fact, the tomcat plugin (red part) in the above plugin can also be directly written in the Goals of build tomcat:run, which is also successful, but the default path and port.

In addition, I use the profiles.xml given to 2.x maven. Note that this file is not supported above 3.0, but there are many other ways to support this important function in 3.0. The files are as follows:

 

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

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
				<abo.fdos>122445045</abo.fdos>
				<nb.wo>fdsao</nb.wo>
            </properties>
        </profile>
        
        <profile>
            <id>test</id>
            <properties>
				<abo.fdos>test...</abo.fdos>
				<nb.wo>fdsao</nb.wo>
            </properties>
        </profile>
    </profiles>
    <activeProfiles>
	</activeProfiles>
</profilesXml>

 Then create a test file under src/main/resources: evn.properties

 

abc=${abo.fdos}

 

After the configuration file is finished, it is time to compile and release the test:

No need to create any tomcat instance in Servers panel

Select Maven Build directly in the menu Run > Debug Configurations... on the left side, then click New, create a new one, and leave it alone, just write in Goals: -X clean tomcat:run , and then write dev in Profiles It is the ID of a profile configuration in profiles.xml.

Then in the Source tab, talk about the project add, you can find the source code during breakpoint debugging.

At this point, that's it, you can click the little bug (debug) in the icon, select the "Maven Build" instance you just created, and publish and start it.

In the above Goals, first clean, then run.

In this way, maven lets tomcat read the class files and resource files under target/classes, that is, target/classes as classpath. And put src\main\webapp as "webroot". So directly modify the jsp page, it will take effect immediately.

And directly modifying the content in the class method will take effect immediately.

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326928164&siteId=291194637