欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用maven-assembly-plugin打包

程序员文章站 2023-12-27 16:43:33
...

有时候希望能把部署环境build在project,这样部属更加方便。

先说遇到的一个错误:

[INFO] No files selected for line-ending conversion or filtering. Skipping: /duitang/dist/app/trunk/java/mytest/src/jetty/resource
[INFO] No files selected for line-ending conversion or filtering. Skipping: /duitang/dist/app/trunk/java/mytest/conf/my
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o  'commons-logging:commons-logging'

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.910s
[INFO] Finished at: Thu Apr 17 11:54:43 CST 2014
[INFO] Final Memory: 9M/208M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single (make-assembly) on project japa: Assembly is incorrectly configured: null: Assembly is incorrectly configured: null:
[ERROR] Assembly: null is not configured correctly: One or more filters had unmatched criteria. Check debug log for more information.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

 

我之前之注意到这句话: 

null is not configured correctly: One or more filters had unmatched criteria. Check debug log for more information.

 

但这句话啥意思也没说。其实关键的是这句:

[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o 'commons-logging:commons-logging'

使用 assembly.xml要求你必须依赖一些包。

基本上包括下面这些都是必须的:

    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
       
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>start</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-util</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jsp-2.1-jetty</artifactId>
            <version>${jetty.version}</version>
        </dependency>
    </dependencies>

 下面是一个完整的配置:

pom.xml

<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.duitang</groupId>
    <artifactId>japa</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>japa</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jetty.version>6.1.26</jetty.version>
        <maven.test.skip>true</maven.test.skip>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
        <maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>
    </properties>
    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
       
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>start</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-util</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jsp-2.1-jetty</artifactId>
            <version>${jetty.version}</version>
        </dependency>
       
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.txt</include>
                    <include>**/*.key</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                        <include>**/*.txt</include>
                        <include>**/*.class</include>
                        <include>**/*.key</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <id>make-a-jar</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
	  
	   <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archiveClasses>true</archiveClasses>
                    <webxml>src/web/web.xml</webxml>
                    <webResources>
                        <resource>
                            <directory>src/web</directory>
                            <targetPath>WEB-INF</targetPath>
                            <filtering>false</filtering>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>		

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <finalName>app</finalName>
                    <attach>false</attach>
                    <descriptors>
                        <descriptor>assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

 

assembly.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1 http://maven.apache.org/xsd/assembly-1.1.1.xsd"
>
    <formats>
        <format>dir</format>
        <!-- format>tar.gz</format -->
	<!-- format>dir</format -->
    </formats>
    <dependencySets>
        <dependencySet>
            <!-- unpack the webapp as root application -->
            <scope>runtime</scope>
            <unpack>true</unpack>
            <outputDirectory>webapps/${artifactId}</outputDirectory>
            <includes>
                <include>${groupId}:${artifactId}:war</include>
            </includes>
        </dependencySet>
        <dependencySet>
            <unpack>false</unpack>
            <outputDirectory>lib</outputDirectory>
            <useTransitiveFiltering>true</useTransitiveFiltering>
            <useStrictFiltering>true</useStrictFiltering>
            <fileMode>666</fileMode>
            <includes>
                <include>org.mortbay.jetty:jetty</include>
                <include>org.mortbay.jetty:jetty-util</include>
                <include>org.mortbay.jetty:start</include>
                <include>org.mortbay.jetty:jsp-2.1-jetty</include>
                <include>commons-logging:commons-logging</include>
                <include>log4j:log4j</include>
            </includes>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>src/jetty/etc</directory>
            <outputDirectory>etc/</outputDirectory>
            <filtered>false</filtered>
        </fileSet>
        <fileSet>
            <directory>src/jetty/logs</directory>
            <outputDirectory>logs/</outputDirectory>
            <filtered>false</filtered>
        </fileSet>
        <fileSet>
            <directory>src/jetty/resource</directory>
            <outputDirectory>resource/</outputDirectory>
            <filtered>true</filtered>
            <includes>
                <include>log4j.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${propsdir}</directory>
            <outputDirectory>webapps/${artifactId}/WEB-INF/</outputDirectory>
            <filtered>true</filtered>
            <excludes>
                <exclude>log4j.properties</exclude>
            </excludes>
        </fileSet>
        <fileSet>
            <directory>src/js</directory>
            <outputDirectory>webapps/${artifactId}/js/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/jetty/bin</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.sh</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
</assembly>

 

另外需要把jetty和web目录放在src中,见附件(如果无需安环境切分,config可以不需要)。

 

补充:下面是精简方案

<?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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<artifactId>tlog-proxy</artifactId>
	<packaging>war</packaging>
	<name>TLog Proxy Server</name>
	<properties> 
		 <jetty.version>6.1.26</jetty.version>   
	</properties>
	<dependencies>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>start</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty-util</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jsp-2.1-jetty</artifactId>
            <version>${jetty.version}</version>
        </dependency>	
	</dependencies>


	<build>
		<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.properties</include>
					<include>**/*.xml</include>
					<include>**/*.txt</include>
					<include>**/*.key</include>
				</includes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.2-beta-5</version>
				<configuration>
					<finalName>tlog-proxy</finalName>
					<attach>false</attach>
					<descriptors>
						<descriptor>assembly.xml</descriptor>
					</descriptors>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	
</project>

 

assembly.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1 http://maven.apache.org/xsd/assembly-1.1.1.xsd"
>
    <formats>
        <format>tar.gz</format>
        <!-- format>tar.gz</format -->
		<!-- format>dir</format -->
    </formats>
    <dependencySets>
        <dependencySet>
            <!-- unpack the webapp as root application -->
            <scope>runtime</scope>
            <unpack>true</unpack>
            <outputDirectory>webapps/${artifactId}</outputDirectory>
            <includes>
                <include>${groupId}:${artifactId}:war</include>
            </includes>
        </dependencySet>
        <dependencySet>
            <unpack>false</unpack>
            <outputDirectory>lib</outputDirectory>
            <useTransitiveFiltering>true</useTransitiveFiltering>
            <useStrictFiltering>true</useStrictFiltering>
            <fileMode>666</fileMode>
            <includes>
                <include>org.mortbay.jetty:jetty</include>
                <include>org.mortbay.jetty:jetty-util</include>
                <include>org.mortbay.jetty:start</include>
                <include>org.mortbay.jetty:jsp-2.1-jetty</include>
                <include>commons-logging:commons-logging</include>
                <include>log4j:log4j</include>
            </includes>
        </dependencySet>
    </dependencySets>
    <fileSets>
    	<fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>webapps/${artifactId}/WEB-INF/classes</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/jetty/etc</directory>
            <outputDirectory>etc/</outputDirectory>
            <filtered>false</filtered>
        </fileSet>
        <fileSet>
            <directory>src/jetty/logs</directory>
            <outputDirectory>logs/</outputDirectory>
            <filtered>false</filtered>
        </fileSet>
        <fileSet>
            <directory>src/jetty/resource</directory>
            <outputDirectory>resource/</outputDirectory>
            <filtered>true</filtered>
            <includes>
                <include>log4j.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${propsdir}</directory>
            <outputDirectory>webapps/${artifactId}/WEB-INF/</outputDirectory>
            <filtered>true</filtered>
            <excludes>
                <exclude>log4j.properties</exclude>
            </excludes>
        </fileSet>
        <fileSet>
            <directory>src/js</directory>
            <outputDirectory>webapps/${artifactId}/js/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/jetty/bin</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.sh</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
</assembly>

 

上一篇:

下一篇: