1 目标
将项目编译完成的内容和本地的文件通过自定义的目录结构打包成war包。
2 程序结构
分为四个项目,packagewar-webapp、packagewar、demo01、demo02,其中后两者是空项目,只是为了作为jar包依赖使用。
packagewar 是 pom 项目,用于执行 maven package 打包,打包完成后将在 packagewar-webapp 的 target 目录下输出 WAR 包。
packagewar-webapp 是打 WAR 包的输出项目,包含自定义的 web 目录结构如下。
packagewar-webapp
∟ src
∟ main
∟ webapp
∟ WEB-INF
∟ lib
∟ debug
∟ deploy
∟ doc
∟ README.md
∟ lib
∟ log
∟ temp
∟ web.xml
∟ index.jsp
∟ pom.xml
3 Maven 的 POM 脚本
- demo01(demo02类似)
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alfred.maven</groupId>
<artifactId>maven-demo01</artifactId>
<version>1.0.0-SNAPSHOT</version>
<description>
概述:用于maven项目研究的demo
作用:可用于配合其他项目进行操作
</description>
</project>
- packagewar
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alfred.maven</groupId>
<artifactId>maven-packagewar</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<description>
概述:【Maven技术研究】打war包
目标:作为总体编译打包的 pom 项目
</description>
<modules>
<module>../com.alfred.maven.demo01</module>
<module>../com.alfred.maven.demo02</module>
<module>../com.alfred.maven.packagewar-webapp</module>
</modules>
</project>
- packagewar-webapp
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alfred.maven</groupId>
<artifactId>maven-packagewar-webapp</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<description>
概述:【Maven技术研究】打war包
目标:将项目编译完成的内容和本地的文件通过自定义的目录结构打包成war包
1)demo01 -> 存放在web前台的lib/debug/deploy目录下
2)demo02 -> 存放在web前台的lib/debug/lib目录下
3)packagewar-webapp -> 打包的web项目
4)保留web项目目录结构不变,集成打包成一个war包PackageWar.war
</description>
<dependencies>
<dependency>
<groupId>com.alfred.maven</groupId>
<artifactId>maven-demo01</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alfred.maven</groupId>
<artifactId>maven-demo02</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<!-- 拷贝lib包下的依赖jar包 -->
<execution>
<id>copy-lib-dependencies</id>
<!-- 在 test-compile 阶段执行 -->
<phase>test-compile</phase>
<goals>
<!-- 阶段目标指定为拷贝依赖 -->
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 依赖包输出到编译路径下的自定义目录 -->
<outputDirectory>${project.build.directory}/${project.artifactId}/WEB-INF/lib/debug/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeArtifactIds>maven-demo02</includeArtifactIds>
</configuration>
</execution>
<!-- 拷贝deploy包下的依赖jar包 -->
<execution>
<id>copy-deploy-dependencies</id>
<phase>test-compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}/WEB-INF/lib/debug/deploy/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeArtifactIds>maven-demo01</includeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<!-- 项目输出目录 -->
<webappDirectory>${project.build.directory}/${project.artifactId}</webappDirectory>
<!-- WAR 包自定义名称 -->
<warName>PackageWar</warName>
<!-- 是否包含空目录,像日志 log、临时 temp 目录 -->
<includeEmptyDirectories>true</includeEmptyDirectories>
<!-- 默认情况下使用 maven-war-plugin 会将依赖的 jar 包都放在 lib 目录下,此处排除 -->
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
4 WAR包结构
PackageWar.war
∟ META-INF
∟ WEB-INF
∟ classes
∟ lib
∟ debug
∟ deploy
∟ maven-demo01-1.0.0-SNAPSHOT.jar
∟ doc
∟ README.md
∟ lib
∟ maven-demo02-1.0.0-SNAPSHOT.jar
∟ log
∟ temp
∟ web.xml
∟ index.jsp
1 目标
将项目编译完成的内容和本地的文件通过自定义的目录结构打包成war包。
2 程序结构
分为四个项目,packagewar-webapp、packagewar、demo01、demo02,其中后两者是空项目,只是为了作为jar包依赖使用。
packagewar 是 pom 项目,用于执行 maven package 打包,打包完成后将在 packagewar-webapp 的 target 目录下输出 WAR 包。
packagewar-webapp 是打 WAR 包的输出项目,包含自定义的 web 目录结构如下。
packagewar-webapp
∟ src
∟ main
∟ webapp
∟ WEB-INF
∟ lib
∟ debug
∟ deploy
∟ doc
∟ README.md
∟ lib
∟ log
∟ temp
∟ web.xml
∟ index.jsp
∟ pom.xml
3 Maven 的 POM 脚本
- demo01(demo02类似)
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alfred.maven</groupId>
<artifactId>maven-demo01</artifactId>
<version>1.0.0-SNAPSHOT</version>
<description>
概述:用于maven项目研究的demo
作用:可用于配合其他项目进行操作
</description>
</project>
- packagewar
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alfred.maven</groupId>
<artifactId>maven-packagewar</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<description>
概述:【Maven技术研究】打war包
目标:作为总体编译打包的 pom 项目
</description>
<modules>
<module>../com.alfred.maven.demo01</module>
<module>../com.alfred.maven.demo02</module>
<module>../com.alfred.maven.packagewar-webapp</module>
</modules>
</project>
- packagewar-webapp
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alfred.maven</groupId>
<artifactId>maven-packagewar-webapp</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<description>
概述:【Maven技术研究】打war包
目标:将项目编译完成的内容和本地的文件通过自定义的目录结构打包成war包
1)demo01 -> 存放在web前台的lib/debug/deploy目录下
2)demo02 -> 存放在web前台的lib/debug/lib目录下
3)packagewar-webapp -> 打包的web项目
4)保留web项目目录结构不变,集成打包成一个war包PackageWar.war
</description>
<dependencies>
<dependency>
<groupId>com.alfred.maven</groupId>
<artifactId>maven-demo01</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alfred.maven</groupId>
<artifactId>maven-demo02</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.2</version>
<executions>
<!-- 拷贝lib包下的依赖jar包 -->
<execution>
<id>copy-lib-dependencies</id>
<!-- 在 test-compile 阶段执行 -->
<phase>test-compile</phase>
<goals>
<!-- 阶段目标指定为拷贝依赖 -->
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 依赖包输出到编译路径下的自定义目录 -->
<outputDirectory>${project.build.directory}/${project.artifactId}/WEB-INF/lib/debug/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeArtifactIds>maven-demo02</includeArtifactIds>
</configuration>
</execution>
<!-- 拷贝deploy包下的依赖jar包 -->
<execution>
<id>copy-deploy-dependencies</id>
<phase>test-compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.artifactId}/WEB-INF/lib/debug/deploy/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeArtifactIds>maven-demo01</includeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<!-- 项目输出目录 -->
<webappDirectory>${project.build.directory}/${project.artifactId}</webappDirectory>
<!-- WAR 包自定义名称 -->
<warName>PackageWar</warName>
<!-- 是否包含空目录,像日志 log、临时 temp 目录 -->
<includeEmptyDirectories>true</includeEmptyDirectories>
<!-- 默认情况下使用 maven-war-plugin 会将依赖的 jar 包都放在 lib 目录下,此处排除 -->
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
4 WAR包结构
PackageWar.war
∟ META-INF
∟ WEB-INF
∟ classes
∟ lib
∟ debug
∟ deploy
∟ maven-demo01-1.0.0-SNAPSHOT.jar
∟ doc
∟ README.md
∟ lib
∟ maven-demo02-1.0.0-SNAPSHOT.jar
∟ log
∟ temp
∟ web.xml
∟ index.jsp