通常我们看社区源码或者下载社区代码安装后发现,社区工程通常有bin、conf、lib等目录,从而代码结构十分清晰,如何做到的呢?
首先,来看看我们平常遇到的痛点。
- 痛点一:war包大,传输费劲。tomcat我们相信老司机最熟悉不过,但你肯定对于最后打完的包含第三方包的war包很头疼,每次传输war包好耗费很多的时间。
- 痛点二:配置打在包里,更新不便。仅仅更新一个配置文件时,都需要重新打包。
---------------华丽的分割线---------------
下面教你新建一个NB的代码工程结构,最终的效果便是,我们有bin(存放脚本)、conf(存放配置)、lib(存放jar包)。
- 1 新建一个maven工程:demo
- 2 工程下新建三个目录:bin、conf、lib。
bin下包含三个脚本:demo-start、demo-stop、demo-restart
conf下包含一个配置模板:demo.properties.template - 3 工程下新建若干子工程,其中包括一个叫packaging的子工程(这个是重点)
- 4 当然你还可以新建一个工程说明文档:README.MD
pom配置:
<?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>org.jeff.r.demo</groupId>
<artifactId>demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>demo-common</module>
<module>demo-service</module>
<module>demo-client</module>
<module>packaging</module>
</modules>
<properties>
<maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
<maven.assembly.plugin.version>2.3</maven.assembly.plugin.version>
<hadoop-23.version>2.7.2-2322</hadoop-23.version>
</properties>
<build>
<pluginManagement>
<plugins>
<!--compiler setting,-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven.assembly.plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
packaging工程配置:
新建目录assembly,并在其下建配置文件bin.xml
bin.xml
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin</id>
<formats>
<format>tar</format>
<format>dir</format>
</formats>
<baseDirectory>demo-${project.version}-bin</baseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<useProjectArtifact>false</useProjectArtifact>
<useStrictFiltering>true</useStrictFiltering>
<useTransitiveFiltering>true</useTransitiveFiltering>
<excludes>
<exclude>org.apache.hadoop:*</exclude>
<exclude>org.slf4j:*</exclude>
</excludes>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.parent.basedir}</directory>
<excludes>
<exclude>**/target/**</exclude>
<exclude>**/.classpath</exclude>
<exclude>**/.project</exclude>
<exclude>**/.setting/**</exclude>
<exclude>lib/**</exclude>
</excludes>
<includes>
<include>README.MD</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<fileMode>755</fileMode>
<directory>${project.parent.basedir}/conf</directory>
<includes>
<include>*</include>
</includes>
<outputDirectory>conf</outputDirectory>
</fileSet>
<fileSet>
<fileMode>755</fileMode>
<directory>${project.parent.basedir}/bin</directory>
<includes>
<include>demo-start.sh</include>
<include>demo-restart.sh</include>
<include>demo-stop.sh</include>
</includes>
<outputDirectory>bin</outputDirectory>
</fileSet>
</fileSets>
</assembly>
packaging.pom
<?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">
<parent>
<artifactId>demo</artifactId>
<groupId>org.jeff.r.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>packaging</artifactId>
<profiles>
<profile>
<id>dist</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assemble</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>demo-${project.version}</finalName>
<descriptors>
<descriptor>src/main/assembly/bin.xml</descriptor>
<!--<descriptor>src/main/assembly/src.xml</descriptor>-->
</descriptors>
<tarLongFileMode>gnu</tarLongFileMode>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.jeff.r.demo</groupId>
<artifactId>demo-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jeff.r.demo</groupId>
<artifactId>demo-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jeff.r.demo</groupId>
<artifactId>demo-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.2-2322</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.2-2322</version>
</dependency>
</dependencies>
</project>
编译打包:
mvn clean package -DskipTests -Pdist
参考文献:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet