Maven打包方式(多模块)

1.概述

  这篇 IntelliJ IDEA自身以及maven项目打包方式(单模块)博文主要是描述项目下单个模块的打包方式,但是现在很多项目往往是多模块组成的,单模块打包方式与多模块差异比较大,所以我们还是有必要学下如何配置多模块打包。

2. Maven打包方式(多模块)

  这里我们主要采用的是maven-assembly-plugins插件进行zip打包。以下面为例:

  

   assembly为打包模块,红框为各大子模块

2.1 maven-assembly-plugins多模块zip打包

2.1.1 父类pom.xml配置

  

   如下:

<?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>com.swordfall</groupId>
  <artifactId>aiplatform</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>aiplatform</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <parent>
    <groupId>org.apache</groupId>
    <artifactId>apache</artifactId>
    <version>21</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <AssemblyPackage.version>1.0-SNAPSHOT</AssemblyPackage.version>
    <!--java-->
    <java.version>1.8</java.version>

    <!--common base-->
    <commons.collections.version>3.2.2</commons.collections.version>
    <commons.httpclient>3.0.1</commons.httpclient>
    <commons.beanutils.version>1.7.0</commons.beanutils.version>
    <commons.configuration.version>1.10</commons.configuration.version>
    <commons.collections4.version>4.1</commons.collections4.version>
    <commons.io.version>2.4</commons.io.version>
    <commons.codec.version>1.6</commons.codec.version>
    <commons.logging.version>1.1.1</commons.logging.version>
    <guava.version>20.0</guava.version>

    <!--maven plugin-->
    <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
    <maven-assembly-plugin.version>3.2.0</maven-assembly-plugin.version>
    <maven-shade-plugin.version>3.2.2</maven-shade-plugin.version>
    <maven-release-plugin.version>2.5.3</maven-release-plugin.version>
    <maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
    <rpm-maven-plugion.version>2.2.0</rpm-maven-plugion.version>
    <maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
    <maven.deploy.skip>false</maven.deploy.skip>
  </properties>

  <dependencyManagement>
    <dependencies>

      <!--base-->
      <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>${commons.codec.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>${commons.logging.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>${commons.collections.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>${commons.httpclient}</version>
      </dependency>
      <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>${commons.beanutils.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils-core</artifactId>
        <version>${commons.beanutils.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>${commons.configuration.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>${commons.collections4.version}</version>
      </dependency>
      <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>${commons.io.version}</version>
      </dependency>
    
      <!--module-->
      <dependency>
        <groupId>com.swordfall</groupId>
        <artifactId>aiplatform-api</artifactId>
        <version>${project.version}</version>
      </dependency>
      <dependency>
        <groupId>com.swordfall</groupId>
        <artifactId>aiplatform-common</artifactId>
        <version>${project.version}</version>
      </dependency>
      <dependency>
        <groupId>com.swordfall</groupId>
        <artifactId>aiplatform-dao</artifactId>
        <version>${project.version}</version>
      </dependency>
      <dependency>
        <groupId>com.swordfall</groupId>
        <artifactId>aiplatform-service</artifactId>
        <version>${project.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>aiplatform-${project.version}</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>rpm-maven-plugin</artifactId>
          <version>${rpm-maven-plugion.version}</version>
          <inherited>false</inherited>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>${java.version}</source>
            <target>${java.version}</target>
            <testSource>${java.version}</testSource>
            <testTarget>${java.version}</testTarget>
          </configuration>
          <version>${maven-compiler-plugin.version}</version>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>${maven-assembly-plugin.version}</version>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>${maven-dependency-plugin.version}</version>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-gpg-plugin</artifactId>
          <version>${maven-gpg-plugin.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-gpg-plugin</artifactId>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>

      <!--编译插件-->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler-plugin.version}</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
          <encoding>${project.build.sourceEncoding}</encoding>
          <skip>true</skip><!--not skip compile test classes-->
        </configuration>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <modules>
    <!-- 父类pom.xml中配置子模块 -->
    <module>aiplatform-rpc</module>
    <module>aiplatform-common</module>
    <module>aiplatform-dao</module>
    <module>aiplatform-service</module>
    <module>aiplatform-api</module>
   <!-- <module>aiplatform-scheduler</module>-->
    <module>assembly</module> <!--打包模块放在最后-->
  </modules>
</project>

2.1.2 子模块aiplatform-api pom.xml配置

  

   配置如下:

<?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>aiplatform</artifactId>
        <groupId>com.swordfall</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>aiplatform-api</artifactId>
    <name>${project.artifactId}</name>
    <packaging>jar</packaging>

    <dependencies>
        <!-- 依赖的其他module -->
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-rpc</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-service</artifactId>
        </dependency>
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-dao</artifactId>
        </dependency>
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-common</artifactId>
        </dependency>
    </dependencies>

    <build>
        <!-- 扫描项目下所有的文件 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

2.1.3 其他子模块pom.xml配置

  其他子模块aiplatform-common、aiplatform-dao、aiplatform-rpc和aiplatform-service的pom.xml配置和aiplatform-api的类似,主要是模块中是否需要调用其他模块的代码,需要则在pom.xml引用其他模块,引用方法参考aiplatform-api的写法

2.1.4 打包模块assembly pom.xml配置(核心)

  

  pom.xml配置如下:

<?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>aiplatform</artifactId>
        <groupId>com.swordfall</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>assembly</artifactId>

    <properties>
        <!--所有的都必须统一,如果为写死的数字,那么都要为数字,否则都要是变量形式,不然会报错-->
        <assembly.name>aiplatform-v${AssemblyPackage.version}</assembly.name>
        <assembly.format>tgz</assembly.format>
        <skipDeploy>true</skipDeploy>
    </properties>

    <dependencies>
        <!-- 引入所有涉及的子模块依赖 -->
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-rpc</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-common</artifactId>
        </dependency>
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-dao</artifactId>
        </dependency>
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-service</artifactId>
        </dependency>
        <dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-api</artifactId>
        </dependency>
        <!--<dependency>
            <groupId>com.swordfall</groupId>
            <artifactId>aiplatform-scheduler</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>-->
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <!-- 配置assembly插件 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${maven-assembly-plugin.version}</version>
                <configuration>
                    <finalName>${assembly.name}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <!-- 配置打包方式assembly.xml文件路径 -->
                        <descriptor>assembly/assembly.xml</descriptor>
                    </descriptors>
                    <!-- 打成的包输出目录 -->
                    <outputDirectory>${project.parent.basedir}/dist</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <!-- 只执行一次 -->
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

  assembly.xml配置为:

<assembly>
    <id>bin</id>
    <!--最终打包成一个用于发布的zip文件-->
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>

    <fileSets>
        <!--模块jar包-->
        <fileSet>
            <directory>${project.parent.basedir}/aiplatform-api/target</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.parent.basedir}/aiplatform-scheduler/target</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.parent.basedir}/aiplatform-common/target</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.parent.basedir}/aiplatform-dao/target</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>

        <!--模块资源resources-->
        <fileSet>
            <directory>${project.parent.basedir}/../config</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.conf</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${project.parent.basedir}/aiplatform-scheduler/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.parent.basedir}/aiplatform-api/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.parent.basedir}/aiplatform-common/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.parent.basedir}/aiplatform-dao/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>

        <!--执行脚本-->
        <fileSet>
            <directory>${project.parent.basedir}/assembly/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>*.sh</include>
            </includes>
        </fileSet>

        <!--logs-->
        <fileSet>
            <directory>${project.parent.basedir}/assembly/logs</directory>
            <outputDirectory>logs</outputDirectory>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <!--不使用项目的artifact,第三方jar不要解压,打包进tar.gz文件的lib目录-->
            <!--配置项目第三方provided、system、runtime不同方式的jar打包进lib目录-->
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>provided</scope>
            <excludes>
                <!--排除assembly模块的jar包-->
                <exclude>com.swordfall:assembly</exclude>
            </excludes>
        </dependencySet>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>system</scope>
            <excludes>
                <exclude>com.swordfall:assembly</exclude>
            </excludes>
        </dependencySet>
        <dependencySet>
            <unpack>false</unpack>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <excludes>
                <!--<exclude>com.swordfall:aiplatform-api</exclude>-->
                <exclude>com.swordfall:assembly</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>

    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <!--配置子模块,按groupId:artifactId配置,然后根据各子模块依赖的顺序排列-->
                <include>com.swordfall:aiplatform-common</include>
                <include>com.swordfall:aiplatform-dao</include>
                <include>com.swordfall:aiplatform-api</include>
                <include>com.swordfall:aiplatform-scheduler</include>
            </includes>
            <binaries>
                <outputDirectory>lib</outputDirectory>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>

</assembly>

  bin目录执行脚本,aiplatform-daemon.sh编写如下:

usage="Usage: aiplatform-daemon.sh (start|stop) <command> "

# if no args specified, show usage
if [ $# -le 1 ]; then
  echo $usage
  exit 1
fi

startStop=$1
shift
command=$1
shift

echo "Begin $startStop $command......"

BIN_DIR=`dirname $0`
BIN_DIR=`cd "$BIN_DIR"; pwd`
AIPLATFORM_HOME=$BIN_DIR/..

source /etc/profile

export JAVA_HOME=$JAVA_HOME
export HOSTNAME=`hostname`

export AIPLATFORM_PID_DIR=$AIPLATFORM_HOME/pid
export AIPLATFORM_LOG_DIR=$AIPLATFORM_HOME/logs
export AIPLATFORM_CONF_DIR=$AIPLATFORM_HOME/config
export AIPLATFORM_LIB_JARS=`find  $AIPLATFORM_HOME/lib/  -name  *.jar | grep -v aiplatform-scheduler-1.0-SNAPSHOT.jar  | xargs |  sed "s/ /:/g"`

export STOP_TIMEOUT=5

if [ ! -d "$AIPLATFORM_LOG_DIR" ]; then
    mkdir $AIPLATFORM_LOG_DIR
fi

log=$AIPLATFORM_LOG_DIR/$command-$HOSTNAME.out
pid=$AIPLATFORM_PID_DIR/$command.pid

cd $AIPLATFORM_HOME

if [ "$command" = "aiplatform" ]; then
    HEAP_INITIAL_SIZE=1g
    HEAP_MAX_SIZE=1g
    HEAP_NEW_GENERATION_SIZE=500m
    LOG_FILE="-Dlogging.config=classpath:logback-api.xml -Dspring.profiles.active=dev"
    CLASS=com.hongshan.aiplatform.api.ApiApplicationServer
else
  echo "Error: No command named $command was found."
  exit 1
fi

export AIPLATFORM_OPTS="-server -Xms$HEAP_INITIAL_SIZE -Xmx$HEAP_MAX_SIZE -Xmn$HEAP_NEW_GENERATION_SIZE -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xss512k -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=128m -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+PrintGCDetails -Xloggc:$AIPLATFORM_LOG_DIR/gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=dump.hprof"

case $startStop in
  (start)
  [ -w "$AIPLATFORM_PID_DIR" ] || mkdir -p "$AIPLATFORM_PID_DIR"

  if [ -f $pid ]; then
      if kill -0 `cat $pid` > /dev/null 2>&1; then
        echo $command running as process `cat $pid`. Stop it first.
        exit 1
      fi
  fi

  echo starting $command, logging to $log

  exec_command="$LOG_FILE $AIPLATFORM_OPTS -classpath $AIPLATFORM_CONF_DIR:$AIPLATFORM_LIB_JARS $CLASS"

  echo "nohup $JAVA_HOME/bin/java $exec_command > $log 2>&1 &"
  nohup $JAVA_HOME/bin/java $exec_command > $log 2>&1 &
  echo $! > $pid
  ;;

 (stop)

   if [ -f $pid ]; then
     TARGET_PID=`cat $pid`
     if kill -0 $TARGET_PID > /dev/null 2>&1; then
       echo stopping $command
       kill $TARGET_PID
       sleep $STOP_TIMEOUT
       if kill -0 $TARGET_PID > /dev/null 2>&1; then
          echo "$command did not stop gracefully after $STOP_TIMEOUT seconds: killing with kill -9"
          kill -9 $TARGET_PID
       fi
     else
       echo no $command to stop
     fi
     rm -f $pid
   else
     echo no $command to stop
   fi
   ;;

 (*)
   echo $usage
   exit 1
   ;;

esac

echo "End $startStop $command."

  start.sh脚本如下:

sh aiplatform-daemon.sh start aiplatform

  stop.sh脚本如下:

sh aiplatform-daemon.sh stop aiplatform

3. 总结

【参考】

https://blog.csdn.net/u013174217/article/details/53300818

https://www.cnblogs.com/yasepix/p/9633793.html

IntelliJ IDEA自身以及maven项目打包方式(单模块)

posted @ 2021-07-22 16:30  牧梦者  阅读(13905)  评论(0编辑  收藏  举报