maven · 2020-09-17 0

maven打war包和jar包及排除文件

一、目录结构

package-test
  src
    main
      java
        com.example.controller
          IndexController.java
      resources
        resources.txt
      webapp
        js
          index.js
        WEB-INF
          web.xml
        index.html
    test
      java
      resources

二、pom文件

pom.xml

<?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.example</groupId>
    <artifactId>package-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <commons.version>2.6</commons.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>${commons.version}</version>
        </dependency>
    </dependencies>
</project>

三、打jar包

1.默认jar配置

1.1 打包

修改 pom.xml 的打包类型 <packaging>jar</packaging>

使用 mvn package 打包

这样不会把依赖jar包打进去,可以使用assembly或dependency插件来解决

1.2 jar包的文件

包内有文件:
package-test/META-INF/MANIFEST.MF
package-test/resources.txt
package-test/com/example/controller/IndexController.class
package-test/META-INF/maven/com.example/package-test/pom.xml
package-test/META-INF/maven/com.example/package-test/pom.properties

zxm@zxm-pc:~/IdeaProjects/package-test/target$ unzip package-test-1.0-SNAPSHOT.jar -d package-test
Archive:  package-test-1.0-SNAPSHOT.jar
   creating: package-test/META-INF/
  inflating: package-test/META-INF/MANIFEST.MF  
   creating: package-test/com/
   creating: package-test/com/example/
   creating: package-test/com/example/controller/
  inflating: package-test/resources.txt  
  inflating: package-test/com/example/controller/IndexController.class  
   creating: package-test/META-INF/maven/
   creating: package-test/META-INF/maven/com.example/
   creating: package-test/META-INF/maven/com.example/package-test/
  inflating: package-test/META-INF/maven/com.example/package-test/pom.xml  
  inflating: package-test/META-INF/maven/com.example/package-test/pom.properties 

2.配置jar插件

2.1 pom文件

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
            <addMavenDescriptor>false</addMavenDescriptor>
            <!-- 主方法所在类名 -->
            <manifest>
                <mainClass>com.example.controller.IndexController</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

2.2 jar包的文件

包内有文件:
package-test/META-INF/MANIFEST.MF
package-test/resources.txt
package-test/com/example/controller/IndexController.class

zxm@zxm-pc:~/IdeaProjects/package-test/target$ unzip package-test-1.0-SNAPSHOT.jar -d package-test
Archive:  package-test-1.0-SNAPSHOT.jar
   creating: package-test/META-INF/
  inflating: package-test/META-INF/MANIFEST.MF  
   creating: package-test/com/
   creating: package-test/com/example/
   creating: package-test/com/example/controller/
  inflating: package-test/resources.txt  
  inflating: package-test/com/example/controller/IndexController.class 

四、打war包

1.默认war配置

1.1 打包

修改 pom.xml 的打包类型 <packaging>war</packaging>

使用 mvn package 打包

1.2 war包的文件

包内有文件:
package-test/META-INF/MANIFEST.MF
package-test/index.html
package-test/WEB-INF/lib/commons-lang-2.6.jar
package-test/WEB-INF/classes/resources.txt
package-test/WEB-INF/classes/com/example/controller/IndexController.class
package-test/WEB-INF/web.xml
package-test/js/index.js
package-test/META-INF/maven/com.example/package-test/pom.xml
package-test/META-INF/maven/com.example/package-test/pom.properties

zxm@zxm-pc:~/IdeaProjects/package-test/target$ unzip package-test-1.0-SNAPSHOT.war -d package-test
Archive:  package-test-1.0-SNAPSHOT.war
   creating: package-test/META-INF/
  inflating: package-test/META-INF/MANIFEST.MF  
   creating: package-test/WEB-INF/
   creating: package-test/WEB-INF/lib/
   creating: package-test/WEB-INF/classes/
   creating: package-test/WEB-INF/classes/com/
   creating: package-test/WEB-INF/classes/com/example/
   creating: package-test/WEB-INF/classes/com/example/controller/
   creating: package-test/js/
  inflating: package-test/index.html  
  inflating: package-test/WEB-INF/lib/commons-lang-2.6.jar  
  inflating: package-test/WEB-INF/classes/resources.txt  
  inflating: package-test/WEB-INF/classes/com/example/controller/IndexController.class  
  inflating: package-test/WEB-INF/web.xml  
  inflating: package-test/js/index.js  
   creating: package-test/META-INF/maven/
   creating: package-test/META-INF/maven/com.example/
   creating: package-test/META-INF/maven/com.example/package-test/
  inflating: package-test/META-INF/maven/com.example/package-test/pom.xml  
  inflating: package-test/META-INF/maven/com.example/package-test/pom.properties  

2.配置war插件

2.1 pom文件

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
        <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
        </archive>
        <!-- packagingExcludes针对拷贝到target后的资源进行处理-->
        <packagingExcludes>
            WEB-INF/classes/**/**,
            WEB-INF/lib/**/**
        </packagingExcludes>
    </configuration>
</plugin>

2.2 war包的文件

包内文件:
package-test/META-INF/MANIFEST.MF
package-test/index.html
package-test/WEB-INF/web.xml
package-test/js/index.js

zxm@zxm-pc:~/IdeaProjects/package-test/target$ unzip package-test-1.0-SNAPSHOT.war -d package-test
Archive:  package-test-1.0-SNAPSHOT.war
   creating: package-test/META-INF/
  inflating: package-test/META-INF/MANIFEST.MF  
   creating: package-test/WEB-INF/
   creating: package-test/js/
  inflating: package-test/index.html  
  inflating: package-test/WEB-INF/web.xml  
  inflating: package-test/js/index.js