Apache Maven WAR Plugin
The WAR Plugin is responsible to collecting all artifact dependencies, classes and the Web application and P Ackaging them into a Web application archive.
The word "pack" sounds more earthy, a more formal version of this should be "building a project package," specifically, to put various files in the project, such as source code, compiled bytecode, configuration files, documents, in accordance with the canonical format to generate the archive, the most common of course is the jar package and the war package, An example of a complex point is the distribution of MAVEN's official download page, which has a custom format that makes it easy for users to decompress and use it on the command line. As a "packaging tool", MAVEN naturally has the obligation to help users create a variety of packages, spec jar packages and war packages naturally no longer words, a slightly more complex custom packaging format must be supported, this article introduces some common packaging cases and related implementations, in addition to some of the packages mentioned earlier, You can also see how to generate source packages, Javadoc packages, and CLI packages that can be run directly from the command line.
Any MAVEN project needs to define the POM element packaging (the default value is a jar if not written). As the name suggests, this element determines how the item is packaged. In the actual case, if you don't declare the element, MAVEN will help you build a jar package; If you define the value of the element as war, you get a war package; If you define a pom (for example, a parent module), then none of the packages will be generated. In addition, MAVEN also supports some other popular packaging formats, such as EJB3 and ear, by default. You don't need to know the specifics of the package, all you have to do is tell maven, "What kind of Project Am I," which is the power of engagement over configuration.
For the same package lifecycle phase, MAVEN calls Maven-jar-plugin for the Jar project, calls Maven-war-plugin for the war project, in other words, packaging directly affects the build lifecycle of maven. It is very important to understand this, especially when you need to customize the packaging behavior, you have to know to configure which plug-ins. Common configuration Parameters archiveclasses configuration items for war plugin
The value of this configuration is True|false and the default is False. Indicates whether the class is packaged.
Normally the war type of the project, the Java code compiled class files will be placed under the Web-inf/classes directory, in bulk. When this parameter is configured to True, all classes are packaged as a Jar,jar name consistent with the name of the war (except for the suffix). The jar is then placed in the Web-inf/lib directory, which is empty at the Web-inf/classes directory. Attachclasses Configuration Items
The value of this configuration is True|false and the default is False. Indicates whether a jar package (only classes, without page-related files) is published at the time the war package is published.
Normally, war-type engineering, when we perform install or deploy, builds a war package, installs it locally or publishes it to the remote.
When this parameter is configured to True, a jar package is added in addition to the war package, but the classifier of the jar package defaults to classes. Overlays Configuration Node
The purpose of the overlays configuration is to merge the contents of the specified war package with the current project. Merge policy: If there is a conflict of the same name, the file in the current project is used. Overlay specific configuration items (include|exclude) can specify files that contain or exclude specific patterns. Configuration Example Multi-war merge
The archiveclasses and attachclasses parameters can be configured to true at the same time. In this case, the package file contains Lib, excluding classes. A jar package with classifier as classes is also published at the time of publication.
If the current project a needs to be combined into the paging file from the B project's war package, the B's class file will also be used in the code. Project B contains the following configuration:
1 2 3 4 5 6 7 8
<plugin> <groupid>org.apache.maven.plugins </groupId> <artifactid>maven-war-plugin </ artifactid> <version>2.6 </version> <configuration> <attachclasses>true </ Attachclasses> </configuration> </plugin>
Project A contains the following configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<dependencies> <dependency> <GROUPID>GROUPB </groupId> <artifactid>b </artifactid > <version>1.0-snapshot </version> <classifier>classes </classifier> <scope> Provided </scope> </dependency> <dependency> <GROUPID>GROUPB </groupId> <artifactid >b </artifactId> <version>1.0-snapshot </version> <type>war </type> </dependency > </dependencies> <build> <plugins> <plugin> <groupid>org.apache.maven.plugins < /groupid> <artifactid>maven-war-plugin </artifactId> <version>2.6 </version> < configuration> <overlays> <overlay> <groupid>groupb </groupId> <artifactid>b </ artifactid> </overlay> </overlays> </configuration> </plugin> </plugins> </build >
Note that if a folder with the same name is merged, no empty folders are merged. How do I exclude the resources directory by packaging the class file into jars?
Normally the war type of the project, the Java code compiled class files will be placed under the Web-inf/classes directory, in bulk. When the archiveclasses parameter is configured to True, all classes are packaged as a Jar,jar name consistent with the name of the war. This jar is then placed in the Web-inf/lib directory, where the web-inf/classes directory is empty, but what to do if you want to preserve the Logback.xml configuration file in the web-inf/classes of the war package.
1 2 3 4 5 6 7 8 9 10 11 12 13-14
<plugin> <artifactid>maven-war-plugin </artifactId> <version> 3.0 0 </version> < configuration> <archiveclasses>true </archiveClasses> <webResources> <resource> < Directory>src/main/resources </directory> <targetpath>web-inf/classes </targetPath> < Filtering>true </filtering> </resource> </webResources> </configuration> </plugin> Dynamic packaging of projects, different environments using different profiles packaged Demo 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
project/'--src | --Main | | --Java | | --Resources | | | --local | | | | --Logback.xml | | | --Spring-datasource.xml | | '--variable.propertes | | | --Product | | | | --Logback.xml | | | --Spring-datasource.xml | | '--variable.propertes | | '--QA | | | --Logback.xml | | --Spring-datasource.xml | | '--variable.propertes | | --WebApp '--Test
1 2
Maven Package–p Local development environment MAVEN PACKAGE–P product production environment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63-64
<profiles> <profile> <id>local </id> <properties> <package.environment>local </package.environment> </properties> <activation> <activebydefault>true </ activebydefault> </activation> </profile> <profile> <id>product </id> < properties> <package.environment>product </package.environment> </properties> </profile > </profiles> <build> <resources> <!--class files are packaged into jar packages, excluding related profiles and Jrebel configuration files--> <resource > <directory>src/main/resources </directory> <excludes> <exclude>local/* </exclude > <exclude>product/* </exclude> <exclude>rebel.xml </exclude> </excludes> < Filtering>true </filtering> </resource> </resources> <plugins> <!-- Distinguish dev Environment from production environment Logback.xml configuration--> <plugin> <groupid>org.apache.maven.plugins </groupId> < artifactid>maven-War-plugin </artifactId> <configuration> <archiveclasses>true </archiveClasses> < Failonmissingwebxml>false </failOnMissingWebXml> <webResources> <resource> <directory> src/main/resources/$ {package.environment} </directory> <filtering>true </filtering> < Targetpath>web-inf/classes </targetPath> </resource> </webResources> </configuration> </plugin> <plugin> <groupid>org.apache.maven.plugins </groupId> <artifactId> Maven-jar-plugin </artifactId> <executions> <execution> <id>default-jar </id> < Phase>package </phase> <goals> <goal>jar </goal> </goals> </execution> </ Executions> </plugin> </plugins> </build> Demo 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20
project/'--src | --Main | | --Java | | --env | | | --Dev | | | | --Log4j.properties | | | --Spring-datasource.xml | | '--variable.propertes | | | --prod | | | --Log4j.properties | | | --Spring-datasource.xml | | '--variable.propertes | | '--QA | | | --Log4j.properties | | --Spring-datasource.xml | | '--variable.propertes | | --Resources | '--webapp '--Test
1 2
MVN clean package-p dev mvn clean package-p QA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130-131
<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> <packaging>war </packaging> <version>0.0.1-snapshot </version > <name>demo Maven Webapp </name> <groupid>webapp </groupId> <artifactid>webapp </ artifactid> <scm> <connection>scm:svn:http://127.0.0.1/dummy </connection> < Developerconnection>scm:svn:https://127.0.0.1/dummy </developerConnection> <tag>head </tag> <url>http://127.0.0.1/dummy </url> </scm> <properties> <spring-version>3.1.0.release </spring-version> </properties> <build> <finalname>$ {final.name} </finalName> < plugins> <plugin> <groupid>org.apache.maven.plugins </groupId> <artifactid> Maven-war-plugin </artifactId> <version>2.1.1 </version> <configuration> <overlays> <overlay> <groupid>transactionresource </groupId> <artifactid>transactionresource </ artifactid> <excludes> <exclude>web-inf/web.xml </exclude> </excludes> </overlay> </overlays> <webResources> <resource> <directory>$ {runtime.env} </directory> < Targetpath>web-inf/classes </targetPath> </resource> </webResources> </configuration> </plugin> </plugins> </build> <dependencies> ... </dependencies> <profiles> < profile> <id>dev </id> <activation> <activebydefault>true </activeByDefault> </ activation> <properties> <runtime.env>src/main/env/dev </runtime.env> <final.name> WebApp </final.name> </properties> <dependencies> <dependency> <grouPid>com.eightqiu </groupId> <artifactid>codecmns </artifactId> <version>0.0.1-snapshot </version> </dependency> </dependencies> </profile> <profile> <id>qa </id> <properties> <runtime.env>src/main/env/qa </runtime.env> <final.name>webapp_$ {BuildNumber } </final.name> </properties> <build> <plugins> <plugin> <groupId> Org.codehaus.mojo </groupId> <artifactid>buildnumber-maven-plugin </artifactId> <version> 1.1 </version> <executions> <execution> <phase>validate </phase> <goals> <goal >create </goal> </goals> </execution> </executions> <configuration> <format> { 0,DATE,YYYYMMDD} </format> <items> <item>timestamp </item> </items> </configuration > </plugin> </plugins> </build> <reporting> <plugins>;p lugin> <groupid>org.apache.maven.plugins </groupId> <artifactid>maven-javadoc-plugin </ artifactid> <version>2.8.1 </version> </plugin> </plugins> </reporting> < dependencies> <dependency> <groupid>com.eightqiu </groupId> <artifactid>codecmns </ artifactid> <version>0.0.1-snapshot </version> <scope>provided </scope> </dependency > </dependencies> </profile> <profile> <id>prod </id> <properties> < Runtime.env>src/main/env/prod </runtime.env> <final.name>webapp </final.name> </properties > <dependencies> <dependency> <groupid>com.eightqiu </groupId> <artifactId> Codecmns </artifactId> <version>0.0.1-snapshot </version> <scope>provided </scope> </dependency> </dependencies> </profile> </profiles> </project>