1. 程式人生 > >Maven分離配置、依賴

Maven分離配置、依賴

在用Maven打包專案時,要像Hadoop、Spark、Hive等專案打包之後的檔案包含bin、lib、conf之類的資料夾,同時可以動態的修改專案的配置引數,需要如下兩步:

  1. pom.xml 檔案中引入 maven-assembly 外掛;
  2. assembly.xml 檔案中制定各個檔案目錄。
pom.xml引入maven-assembly外掛
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>true</appendAssemblyId>
        <descriptors>
            <descriptor>src/assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>maven-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
然後,是src/assembly/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">

    <id>all</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>bin</directory>
            <outputDirectory>bin</outputDirectory>
            <fileMode>755</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>conf</outputDirectory>
            <fileMode>755</fileMode>
            <lineEnding>unix</lineEnding>
            <excludes>
                <exclude>*.formatted</exclude>
            </excludes>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <fileMode>755</fileMode>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <useProjectArtifact>false</useProjectArtifact>
            <excludes>
                <exclude>${project.groupId}:${project.artifactId}</exclude>
            </excludes>
        </dependencySet>
        <dependencySet>
            <fileMode>755</fileMode>
            <outputFileNameMapping>${project.artifactId}.jar</outputFileNameMapping>
            <outputDirectory>/lib/</outputDirectory>
            <scope>runtime</scope>
            <includes>
                <include>${project.groupId}:${project.artifactId}</include>
            </includes>
        </dependencySet>
    </dependencySets>
</assembly>

不難發現,我們把打好的程式jar包也放到了 lib目錄 中。

執行程式碼 用這種方法打的jar包,是不包含依賴的jar包和配置檔案的。所以,在執行之前需要把依賴的jar包和配置檔案加入到 $CLASSPATH 中,Shell指令碼如下:
#!/usr/bin/env bash

BIN=`dirname $0`
BIN=`cd $BIN; pwd`
APP_HOME=`dirname $BIN`

JAVA=${JAVA_HOME}/bin/java
CLASSPATH=${APP_HOME}/conf
#JAR=${APP_HOME}/tools.jar

for f in ${APP_HOME}/lib/*.jar; do
    CLASSPATH=${CLASSPATH}:$f
done

for f in ${APP_HOME}/*.jar; do
    CLASSPATH=${CLASSPATH}:$f
done

MAINCLASS="edu.wzm.joda.JodaDemo"
exec "$JAVA" -classpath $CLASSPATH $MAINCLASS