eclipse中maven编译发布程序在tomcat进行调试,并替换资源文件中的替换符

目标:使用eclipse,通过maven来编译并发布程序到tomcat,并需支持断点调试,以及资源文件中替换符号的过滤。

原理就是 通过maven来编译并替换资源文件的替换符,然后调用tomcat启动embeded (内嵌)实例,来测试程序。

下面是 pom.xml 文件样子

<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>

 其中用了插件 “maven-compiler-plugin” 指定编译版本是 1.6,貌似不指定会导致一些5.0以上jdk的特性语法不支持(如@Override) 。

然后使用了插件“tomcat-maven-plugin”,修改其中的  path 和端口为自己想要的即可。

插件 maven-resources-plugin 也不能少,如上配置即可。

特别注意的是 在 build 标签里面有个 resources 标签,再里面有  resource ,directory 指定的是默认的资源文件目录,关键在于 filtering 这个设置为true,maven就会在将资源文件拷贝到 target 目录时进行替换符替换,讲上面 evn.properties文件中的 ${abo.fdos} 替换掉,替换的内容默认就是使用 pom.xml统计目录下的 profiles.xml 里的配置,配置文件看下面即可。当然这个文件是 maven2.x 的,3.x 不支持。替换符的内容还可以通过在pom.xml 里写的profile标签里指定,等,还有很多其他方式指定profile内容。

然后需要在eclipse 的 windows > preferences > server > Runtime Environment 里配置tomcat 。

当maven编译(compile)时,会出现 找不到 javax.servlet.Servlet类 情况,即便是 在 eclipse 里的 Java Build Path 里 的 Libraries 里添加上 tomcat ,也没用。那么我们需要在pom.xml里配置 依赖 servlet-api. 特别注意的是 scope 需要指定为 provided ,如果不写,或者是 compile 什么的,我测试时,出现了运行时异常,访问Servlet时,说 我的Servlet 类无法转换成 javax.servlet.Servlet 。provided 可能就是在编译时使用,但是发布时不讲Servlet-api.jar 发布出去,而是使用容器(tomcat)自身的servlet-api.jar。

 新增备注:经后面测试,其实上面plugin中的 tomcat 的plugin (红色部分) 不要也是可以直接在build 的Goals 中写 tomcat:run ,居然也是成功的,不过就是默认的 path 和 port 了。

另外我用的是 给予2.x maven 的 profiles.xml ,注意3.0 以上不支持这个文件了,不过3.0 可以有其他很多办法支持这一重要功能,文件如下:

<?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>

 然后创建一个测试文件 在 src/main/resources 下:evn.properties

abc=${abo.fdos}

配置的文件说完了,然后就是进行编译发布测试:

无需再Servers 面板里创建任何 tomcat 实例

直接在 菜单中 Run > Debug Configurations... 里左侧选择 Maven Build,然后点击新建,新建一个,其他地方就不说,就是 Goals 里写上: -X clean  tomcat:run ,然后Profiles 里写上dev 就是 profiles.xml里的 一个profile配置的 ID。

然后在 Source 选项卡里,讲工程add进来,就可以在断点调试时找到源码。

至此,就可以了,就可以点击 图标中的 小虫子(debug),选择刚才创建的 “Maven Build” 实例,就进行了发布并启动了。

上面Goals 里,先是 clean ,然后 run 。

这样使用,maven 让 tomcat 读取的是 target/classes 下的类文件和资源文件,也就是target/classes 作为 classpath。并将 src\main\webapp  作为“webroot”。所以直接修改jsp页面,会立马生效。

而且直接修改class 方法里的内容,也是立即生效的。

猜你喜欢

转载自kangzye.iteye.com/blog/2297130
今日推荐