[Maven] maven configuration for webapp environment

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

        <!-- ================================================= -->
        <!-- Log and related dependencies (use slf4j+logback instead of jcl+log4j) -->
        <!-- ================================================= -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- slf4j implementation: logback, used to replace log4j. Faster and stronger! -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>

        <!-- Convert existing jakarta commons logging calls to slf4j calls. -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <!-- Hack: Make sure the commons-logging jar package is not imported, otherwise it will conflict with 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>
            <!-- First specify all files and folders under src/main/resources as resource files -->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
            <!-- Set the *.properties, logback.xml to be considered, that is, the ${key} in these files will be replaced -->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.properties</include>
                    <include>logback.xml</include>
                </includes>
            </resource>
        </resources>

        <!-- Official website 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>
                    <!-- charset encoding-->
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                    <!-- The development version used by the source code -->
                    <source>1.6</source>
                    <!-- The compiled version of the target class file to be generated -->
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <!-- The default exploded WAR directory is target/em-1.0-SNAPSHOT (the format is <artifactId>-<version>)
                        After modification, the directory becomes target/exploded/em.war -->
                    <webappDirectory>target/${warExplodedDirectory}</webappDirectory>
                    <!-- The name of the packaged war file, the default is <artifactId>-<version>.war -->
                    <warName>${finalName}</warName>
                    <webResources>
                        <resource>
                            <!-- Enable filter auto config, build/filters will override configuration/filters under maven-war-plugin plugin -->
                            <filtering>true</filtering>
                            <!-- Specify the resource directory (default directory src/main/resources) -->
                            <directory>${basedir}/src/main/webapp</directory>
                            <includes>
                                <!-- list of files that auto config will replace -->
                                <include>**/*.xml</include>
                                <include>**/*.properties</include>
                            </includes>
                            <!-- Specify the copy destination of the files in the resource directory-->
                            <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 example
<?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>


A few notes:
1. The maven-compiler-plugin plugin, by default, outputs the compiled class file to target\classes
2. The maven-war-plugin plugin, the command line mvn war:war (it just copies the resource file to the target location) , if there is no target\classes folder at this time, no error will be prompted when the war command is executed, so it is generally bundled together to execute mvn compile war:war
3, maven-assembly-plugin plug-in, usually to configure an automated release script or system Use the command line mvn assembly:assembly, which is used to package a folder as a tgz file. Generally used for project deployment or archiving

Guess you like

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