Ticket #8 (new 分享)

Opened 14 years ago

Last modified 13 years ago

Maven打包

Reported by: qinhongyuan Owned by: qinhongyuan
Priority: major Milestone: Maven插件
Component: Maven教程 Version:
Keywords: Maven 打包 assembly Cc: qinhongyuan
Due Date: 06/02/2012

Description

讲解一下Maven强大插件之一的分发包插件(maven-assembly-plugin)

Change History

comment:1 Changed 14 years ago by qinhongyuan

  • 简单说一下,项目中有不想打包的文件可以使用这种方式
    	    <plugin>
    		<groupId>org.apache.maven.plugins</groupId>
    		<artifactId>maven-war-plugin</artifactId>
    		<version>2.1.1</version>
    		<configuration>
    		    <packagingExcludes>js/**,cache/**,images/**</packagingExcludes>
    		    <failOnMissingWebXml>false</failOnMissingWebXml>
    		</configuration>
    	    </plugin>
    
  • war打包插件里面可以配置打包不包含的文件,这里的配置就是说js,cache,images的所有文件不参与打包

  • 分发包插件的使用(编写statics-zip.xml,这里我就和pom.xml放在同一级目录吧)
    <?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>statics</id>
    	<formats><format>zip</format></formats>
    	<includeBaseDirectory>false</includeBaseDirectory>
    	<fileSets>
    	    <fileSet>
    	      <directory>${basedir}/src/main/webapp/js</directory>
    	      <outputDirectory>js</outputDirectory>
    	    </fileSet>
    	    <fileSet>
    	      <directory>${basedir}/src/main/webapp/cache</directory>
    	      <outputDirectory>cache</outputDirectory>
    	    </fileSet>
    	    <fileSet>
    	      <directory>${basedir}/src/main/webapp/images</directory>
    	      <outputDirectory>images</outputDirectory>
    	    </fileSet>		
    	</fileSets>
    </assembly>
    
  • 这个文件是说,images,js,cache这三个目录另外打成一个zip包
  • 静态文件放在nginx机器,所以打包分开打了

  • 在pom.xml添加分发包插件
    	    <plugin>
    		<groupId>org.apache.maven.plugins</groupId>
    		<artifactId>maven-assembly-plugin</artifactId>
    		<version>2.2</version>
    		<executions>
    		    <execution>
    			<id>make-assembly</id>
    			<phase>package</phase>
    			<goals>
    			    <goal>single</goal>
    			</goals>
    			<configuration>
    			    <descriptors>
    				<descriptor>statics-zip.xml</descriptor>
    			    </descriptors>
    			</configuration>
    		    </execution>
    		</executions>
    	    </plugin>
    
  • 构建项目的时候,maven会自动打分发包的

comment:2 Changed 14 years ago by qinhongyuan


  • 有时候我们需要打一些jar给脚本,当然希望生成的jar会自动完成
  • 这个是论坛用户行为分析的一个jar
    <?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>userAnalyse</id>
    	<formats><format>jar</format></formats>
    	<includeBaseDirectory>false</includeBaseDirectory>
    	<fileSets>
    	    <fileSet>
    	      <directory>${basedir}/target/webapp/WEB-INF/classes</directory>
    	      <includes>
    		  <include>
    		      **/cn/pconline/bbs6/analyse/**
    		  </include>
    		  <include>**/mongo.properties</include>
    	      </includes>
    	      <outputDirectory>/</outputDirectory>
    	    </fileSet>
    	</fileSets>
    </assembly>
    

comment:3 Changed 13 years ago by qinhongyuan

  • 源码打包,需要添加这个插件
    <plugins>
               <!--   要将源码放上去,需要加入这个插件    -->
                <plugin>  
                    <artifactId>maven-source-plugin</artifactId>  
                    <version>2.1</version>  
                    <configuration>  
                        <attach>true</attach>  
                    </configuration>  
                    <executions>  
                        <execution>  
                            <phase>compile</phase>  
                            <goals>  
                                <goal>jar</goal>  
                            </goals>  
                        </execution>  
                    </executions>  
                </plugin>            
    </plugins>
    
Note: See TracTickets for help on using tickets.