【Maven】webapp环境的maven配置

<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>com.xxx.em</groupId>
    <artifactId>em</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>em</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <finalName>em</finalName>
        <warName>${finalName}.war</warName>
        <warExplodedDirectory>exploded/${warName}</warExplodedDirectory>

        <logback.version>1.1.3</logback.version>
        <slf4j.version>1.7.6</slf4j.version>
    </properties>

    <dependencies>

        <!-- ================================================= -->
        <!-- 日志及相关依赖(用slf4j+logback代替jcl+log4j) -->
        <!-- ================================================= -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- slf4j的实现:logback,用来取代log4j。更快、更强! -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>

        <!-- 将现有的jakarta commons logging的调用转换成slf4j的调用。 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!-- Hack:确保commons-logging的jar包不被引入,否则将和jcl-over-slf4j冲突 -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>

        <filters>
            <filter>filter.properties</filter>
        </filters>
        <resources>
            <!-- 先指定src/main/resources下所有文件和文件夹为资源文件 -->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
            <!-- 设置对*.properties,logback.xml进行过虑,即这些文件中的${key}会被替换 -->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.properties</include>
                    <include>logback.xml</include>
                </includes>
            </resource>
        </resources>

        <!-- 官网 http://maven.apache.org/plugins/index.html -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <!-- 字符集编码 -->
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                    <!-- 源代码使用的开发版本 -->
                    <source>1.6</source>
                    <!-- 需要生成的目标class文件的编译版本 -->
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <!-- 默认exploded WAR目录为 target/em-1.0-SNAPSHOT(格式为<artifactId>-<version>)
                        修改以后目录变成target/exploded/em.war -->
                    <webappDirectory>target/${warExplodedDirectory}</webappDirectory>
                    <!-- 打包后的war文件名,默认是 <artifactId>-<version>.war -->
                    <warName>${finalName}</warName>
                    <webResources>
                        <resource>
                            <!-- 启用filter的auto config,build/filters会覆maven-war-plugin插件下configuration/filters -->
                            <filtering>true</filtering>
                            <!-- 指定资源目录(默认目录src/main/resources) -->
                            <directory>${basedir}/src/main/webapp</directory>
                            <includes>
                                <!-- auto config会替换的文件列表 -->
                                <include>**/*.xml</include>
                                <include>**/*.properties</include>
                            </includes>
                            <!-- 指定该资源目录下的文件的拷贝目标位置 -->
                            <targetPath>/</targetPath>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.5</version>
                <!--<executions>-->
                    <!--<execution>-->
                        <!--<id>assembly</id>-->
                        <!--<phase>package</phase>-->
                        <!--<goals>-->
                            <!--<goal>single</goal>-->
                        <!--</goals>-->
                    <!--</execution>-->
                <!--</executions>-->
                <configuration>
                    <finalName>${finalName}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptor>assembly.xml</descriptor>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


assembly.xml 示例
<?xml version="1.0" encoding="UTF-8"?>
<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

	<id>em-dist</id>
	<baseDirectory>${warName}</baseDirectory>
	<formats>
		<format>tgz</format>
	</formats>

	<includeBaseDirectory>true</includeBaseDirectory>
	<fileSets>
		<fileSet>
			<directory>target/exploded/${warName}</directory>
			<includes>
				<include>**</include>
			</includes>
			<outputDirectory>/</outputDirectory>
		</fileSet>
	</fileSets>

</assembly>


几点说明:
1、maven-compiler-plugin插件,默认输出到编译后的class文件到 target\classes
2、maven-war-plugin插件,命令行  mvn war:war(它只是拷贝资源文件到目标位置),如果此时没有target\classes文件夹,执行war命令时,也不会提示错误,故一般绑在一起执行mvn compile war:war
3、maven-assembly-plugin插件,一般是配置自动化发布脚本或系统使用,命令行 mvn assembly:assembly ,其作用是将某文件夹打包为 tgz文件。一般用于项目部署或归档

你可能感兴趣的:(maven)