Maven打包war和ear

在软件开发过程中,我们需要经常对项目进行打包并部署。这里的打包即-构建项目软件包。说白了,其实就是对项目中的各种文件,比如编译生成的字节码、配置文件、文档等,根据maven规范的格式或者配置的要求生成归档。最常见的就是对JAR包和WAR包打包。后者是一般都是web 项目。


通常情况下,我们的项目可能很大,也会划分成web 项目模块和一些后台模块。这个时候一般都是将web 项目打包成 war 包,然后再所有项目包括 jar包 打包成ear 包。maven都提供了不止一种方式,这里只使用最直观的一种方式,即mave的 maven-war-plugin 插件和 maven-ear-plugin 插件。下面通过一个例子解释下:
先介绍下项目结构:

mavenaggregator(项目根目录)  
   |----another-project ——代表项目其他模块
   |----my-project      ——代表项目web 模块
   |----projectear      ——打包 ear 的模块
   |----webwar          ——打包 war 的模块
   |----pom.xml         ——聚合项目的pom文件,用于一次构建整个项目
解释:这里创建一个聚合项目,然后在其中创建两个子模块,分别用于打包 war 和 ear。这样,每次构建整个聚合项目时,就能得到我们想要的 ear 包了。



war
在webwar 项目的 pom.xml 中添加如下配置:

<!-- 构建项目所需要的信息 -->  
    <build>
    	<finalName>myprojectwar</finalName>
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-war-plugin</artifactId>  
                <version>2.1.1</version>
                <configuration>
                	<webResources>
			            <resource>
			            	<directory>src/main/webapp</directory>
			            	<targetPath>verification</targetPath>
			            	<filtering>true</filtering>
			            </resource>
			        </webResources>
                </configuration> 
            </plugin>  
        </plugins>  
    </build>  
这里配置也指定了web项目的前台文件夹。


ear
首先,因为我们是要将整个项目都打包成一个ear 包,因此我们的projectear 模块会依赖其他模块,添加依赖如下:

<!-- 其他代码 -->  
    <dependencies>  
        <dependency>  
            <groupId>com</groupId>  
            <artifactId>my-project</artifactId>  
            <version>${project.version}</version>
            <type>jar</type>  
        </dependency>  
  
        <dependency>  
            <groupId>com</groupId>  
            <artifactId>another-project</artifactId>  
            <version>${project.version}</version>
            <type>jar</type>  
        </dependency>  
        <dependency>  
            <groupId>com</groupId>  
            <artifactId>webwar</artifactId>  
            <version>${project.version}</version>
            <type>war</type>  
        </dependency>  
    </dependencies>  
<!-- 其他代码 -->
然后在pom的build元素中配置 maven-ear-plugin 插件,同时指定要打包到ear 文件的jar包和资源。

<!-- 其他代码 -->
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-ear-plugin</artifactId>  
                <version>2.9</version>  
                <configuration>
                    <!-- 指定要打包的 jar包和资源文件 -->
                    <packagingIncludes>META-INF/**,**/*.jar,**/*.war</packagingIncludes>  
                    
                    <!-- 指定要打包进来的模块 -->
                    <modules> 
                        <jarModule>  
                            <groupId>com</groupId>  
                            <artifactId>another-project</artifactId>  
                            <includeInApplicationXml>true</includeInApplicationXml>  
                        </jarModule>  
                        <jarModule>  
                            <groupId>com</groupId>  
                            <artifactId>my-project</artifactId>  
                            <includeInApplicationXml>true</includeInApplicationXml>  
                        </jarModule>  
                        <webModule>  
                            <groupId>com</groupId>  
                            <artifactId>webwar</artifactId>  
                        </webModule>  
                    </modules>
  
                </configuration>  
  
            </plugin>  
        </plugins>  
    </build>  
<!-- 其他代码 -->

最后回到项目根目录对整个项目进行一次打包即可:

mvn clean package
完整代码








猜你喜欢

转载自blog.csdn.net/hustzw07/article/details/78290251