首页 > 代码库 > maven package自动部署包

maven package自动部署包

我们部署项目时经常会遇到打包部署工作(非web工程),那该如何配置pom.xml?请看如下配置

1.工程目录



2.pom配置

2.1  注意只需要配置pom中build节点就可以了。
    <build>
	<!-- 打包工程名称  -->
        <finalName>mafka-test</finalName>
	<!-- 拷贝所有依赖jar到部署工程目录下 -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/${build.finalName}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
	    <!-- 配置相关文件到部署目录下 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <encoding>UTF-8</encoding>
                            <outputDirectory>${basedir}/${build.finalName}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <includes>
                                        <include>*.*</include>
                                    </includes>
                                    <filtering>false</filtering>
                                </resource>
                                <resource>
                                    <directory>src/main/resources/config/</directory>
                                    <targetPath>${basedir}/${build.finalName}/config</targetPath>
                                    <includes>
                                        <include>*.*</include>
                                    </includes>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- 打jar包时需要把配置文件给排除在外 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>1.0</classifier>
                            <outputDirectory>${basedir}/${build.finalName}/lib</outputDirectory>
                            <excludes>
                                <exclude>*.xml</exclude>
                                <exclude>*.properties</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


3.构建结果

/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/bin/java -Dclassworlds.conf=/usr/local/Cellar/maven/3.2.1/libexec/bin/m2.conf -Dmaven.home=/usr/local/Cellar/maven/3.2.1/libexec -Didea.launcher.port=7543 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 12.app/bin" -Dfile.encoding=UTF-8 -classpath "/usr/local/Cellar/maven/3.2.1/libexec/boot/plexus-classworlds-2.5.1.jar:/Applications/IntelliJ IDEA 12.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher --no-plugin-registry --fail-fast --strict-checksums --update-snapshots -DskipTests=true package -P nexus,nexus-snapshots
[WARNING] Command line option -npr is deprecated and will be removed in future Maven versions.
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.meituan.mafka-test:mafka-test:jar:1.0-SNAPSHOT
[WARNING] ‘dependencies.dependency.(groupId:artifactId:type:classifier)‘ must be unique: junit:junit:jar -> version 4.7 vs 4.11 @ line 149, column 21
[WARNING] The expression ${build.finalName} is deprecated. Please use ${project.build.finalName} instead.
[WARNING] The expression ${build.finalName} is deprecated. Please use ${project.build.finalName} instead.
[WARNING] The expression ${build.finalName} is deprecated. Please use ${project.build.finalName} instead.
[WARNING] The expression ${build.finalName} is deprecated. Please use ${project.build.finalName} instead.
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building mafka-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://nexus:8081/nexus/content/groups/public-snapshots/com/meituan/mtrace/mtrace/1.0.5-SNAPSHOT/maven-metadata.xml
Downloaded: http://nexus:8081/nexus/content/groups/public-snapshots/com/meituan/mtrace/mtrace/1.0.5-SNAPSHOT/maven-metadata.xml (361 B at 2.0 KB/sec)
Downloading: http://nexus:8081/nexus/content/groups/public-snapshots/com/meituan/mafka/client/mafka-client/1.0.0-SNAPSHOT/maven-metadata.xml
Downloaded: http://nexus:8081/nexus/content/groups/public-snapshots/com/meituan/mafka/client/mafka-client/1.0.0-SNAPSHOT/maven-metadata.xml (373 B at 3.5 KB/sec)
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ mafka-test ---
[debug] execute contextualize
[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ mafka-test ---
[INFO] Compiling 4 source files to /Users/lizhitao/mt_wp/test_wp/kafka-test/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ mafka-test ---
[debug] execute contextualize
[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/lizhitao/mt_wp/test_wp/kafka-test/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ mafka-test ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mafka-test ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ mafka-test ---
[INFO] Building jar: /Users/lizhitao/mt_wp/test_wp/kafka-test/target/mafka-test.jar
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (copy-dependencies) @ mafka-test ---
[INFO] Copying mafka-client-1.0.0-SNAPSHOT.jar to /Users/lizhitao/mt_wp/test_wp/kafka-test/mafka-test/lib/mafka-client-1.0.0-SNAPSHOT.jar
[INFO] Copying mtrace-1.0.5-SNAPSHOT.jar to /Users/lizhitao/mt_wp/test_wp/kafka-test/mafka-test/lib/mtrace-1.0.5-SNAPSHOT.jar
[INFO] core-3.1.1.jar already exists in destination.
[INFO] curator-framework-1.0.1.jar already exists in destination.
[INFO] oro-2.0.8.jar already exists in destination.
[INFO] commons-net-1.4.1.jar already exists in destination.
[INFO] netty-3.2.1.Final.jar already exists in destination.
[INFO] json-simple-1.1.jar already exists in destination.
[INFO] guava-10.0.1.jar already exists in destination.
[INFO] commons-logging-1.1.1.jar already exists in destination.
[INFO] commons-lang-2.6.jar already exists in destination.
[INFO] curator-client-1.0.1.jar already exists in destination.
[INFO] org.springframework.asm-3.1.1.RELEASE.jar already exists in destination.
[INFO] jsp-2.1-6.1.14.jar already exists in destination.
[INFO] scala-library-2.8.0.jar already exists in destination.
[INFO] commons-math-2.0.jar already exists in destination.
[INFO] jline-0.9.94.jar already exists in destination.
[INFO] org.springframework.beans-3.1.1.RELEASE.jar already exists in destination.
[INFO] commons-httpclient-3.1.jar already exists in destination.
[INFO] jopt-simple-4.3.jar already exists in destination.
[INFO] jackson-mapper-asl-1.5.5.jar already exists in destination.
[INFO] metrics-annotation-2.2.0.jar already exists in destination.
[INFO] xmlenc-0.52.jar already exists in destination.
[INFO] jets3t-0.7.1.jar already exists in destination.
[INFO] jetty-util-6.1.14.jar already exists in destination.
[INFO] pig-0.8.0.jar already exists in destination.
[INFO] metrics-core-2.2.0.jar already exists in destination.
[INFO] kafka_2.10-0.8.1.jar already exists in destination.
[INFO] commons-cli-1.2.jar already exists in destination.
[INFO] jackson-core-asl-1.5.5.jar already exists in destination.
[INFO] hsqldb-1.8.0.10.jar already exists in destination.
[INFO] zookeeper-3.3.3.jar already exists in destination.
[INFO] hadoop-core-0.20.2.jar already exists in destination.
[INFO] junit-4.11.jar already exists in destination.
[INFO] paranamer-2.2.jar already exists in destination.
[INFO] jsr305-1.3.9.jar already exists in destination.
[INFO] log4j-1.2.16.jar already exists in destination.
[INFO] servlet-api-2.5-6.1.14.jar already exists in destination.
[INFO] paranamer-ant-2.2.jar already exists in destination.
[INFO] kafka_2.8.0-0.8.1.jar already exists in destination.
[INFO] snappy-java-1.0.5.jar already exists in destination.
[INFO] kafka-hadoop-consumer-0.8.1.jar already exists in destination.
[INFO] org.json.simple-0.3-incubating.jar already exists in destination.
[INFO] jsp-api-2.1-6.1.14.jar already exists in destination.
[INFO] hamcrest-core-1.3.jar already exists in destination.
[INFO] jasper-compiler-5.5.12.jar already exists in destination.
[INFO] kfs-0.3.jar already exists in destination.
[INFO] jasper-runtime-5.5.12.jar already exists in destination.
[INFO] commons-codec-1.4.jar already exists in destination.
[INFO] commons-collections-3.2.1.jar already exists in destination.
[INFO] asm-3.2.jar already exists in destination.
[INFO] jetty-6.1.14.jar already exists in destination.
[INFO] zkclient-0.3.jar already exists in destination.
[INFO] qdox-1.10.1.jar already exists in destination.
[INFO] velocity-1.6.4.jar already exists in destination.
[INFO] avro-1.4.0.jar already exists in destination.
[INFO] ant-1.7.1.jar already exists in destination.
[INFO] ant-launcher-1.7.1.jar already exists in destination.
[INFO] paranamer-generator-2.2.jar already exists in destination.
[INFO] avro-1.3.2.jar already exists in destination.
[INFO] curator-test-1.0.1.jar already exists in destination.
[INFO] service-common-mtzkclient-1.6.1.jar already exists in destination.
[INFO] slf4j-api-1.6.1.jar already exists in destination.
[INFO] slf4j-log4j12-1.6.1.jar already exists in destination.
[INFO] commons-el-1.0.jar already exists in destination.
[INFO] ant-1.6.5.jar already exists in destination.
[INFO] org.springframework.core-3.1.1.RELEASE.jar already exists in destination.
[INFO] javassist-3.15.0-GA.jar already exists in destination.
[INFO] curator-recipes-1.0.1.jar already exists in destination.
[INFO] 
[INFO] --- maven-resources-plugin:2.5:copy-resources (copy-resources) @ mafka-test ---
[debug] execute contextualize
[INFO] Using ‘UTF-8‘ encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources to /Users/lizhitao/mt_wp/test_wp/kafka-test/mafka-test/config
[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default) @ mafka-test ---
[INFO] Building jar: /Users/lizhitao/mt_wp/test_wp/kafka-test/mafka-test/lib/mafka-test-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.871 s
[INFO] Finished at: 2014-05-14T14:41:53+08:00
[INFO] Final Memory: 25M/228M
[INFO] ------------------------------------------------------------------------

如下图: