美文网首页
java项目打包配置

java项目打包配置

作者: 雨中星辰0 | 来源:发表于2022-01-26 20:36 被阅读0次

java项目打包

作者 时间
雨中星辰 2022-01-26

非springboot版

pom.xml


    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <!-- 该插件用于指定jar包编译和运行版本 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- 该插件用于将项目打成jar包 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <!--不打包资源文件-->
                    <excludes>
                        <exclude>*</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                            <classpathPrefix>./lib/</classpathPrefix>
                            <!--jar包不包含唯一版本标识-->
                            <useUniqueVersions>false</useUniqueVersions>
                            <!--指定入口类-->
                            <mainClass>com.epri.zeus.cpp.ZeusCppNaticeProvider</mainClass>
                        </manifest>
                        <manifestEntries>
                            <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                            <!--本地依赖包需要手动 加入Class-Path ,否则无法找到-->
                            <Class-Path>./conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <!-- 资源插件,用于在package时,将src/main/resources拷贝到target/conf目录 -->
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/conf</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <!-- 该插件用于打发布包 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <!--打包后的文件名称-->
                    <finalName>${project.artifactId}-${project.parent.version}</finalName>
                    <descriptors>
                        <!--assembly打包的配置文件--->
                        <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>

springboot版

pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>${commons-jexl3.version}.2</version>
                <configuration>
                    <!--不打包资源文件-->
                    <excludes>
                        <exclude>*.**</exclude>
                        <exclude>static/</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                            <classpathPrefix>./lib/</classpathPrefix>
                            <!--jar包不包含唯一版本标识-->
                            <useUniqueVersions>false</useUniqueVersions>
                            <!--指定入口类-->
                            <mainClass>com.epri.poseidon.AdminApplication</mainClass>
                        </manifest>
                        <manifestEntries>
                            <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                            <!--本地依赖包需要手动 加入Class-Path ,否则无法找到-->
                            <Class-Path>./conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </configuration>
            </plugin>

            <plugin>
                <!-- 资源插件,用于在package时,将src/main/resources拷贝到target/conf目录 -->
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                </resource>
                            </resources>
                            <outputDirectory>${project.build.directory}/conf</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.4</version>
                <configuration>
                    <!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖-->
                    <includes>
                        <include>
                            <groupId>null</groupId>
                            <artifactId>null</artifactId>
                        </include>
                    </includes>
                    <layout>ZIP</layout>
                    <!--使用外部配置文件,jar包里没有资源文件-->
                    <addResources>true</addResources>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <!--打包后的文件名称-->
                    <finalName>${project.artifactId}-${project.parent.version}</finalName>
                    <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>

多环境打包配置

有的时候,我们需要管理多个环境,比如开发环境、测试环境、uat环境等,面对多环境等情况下,通常是两种方式:1.使用maven的多环境配置(本文采用方式);2.springboot项目可以配置多环境配置,但该方式只能设置springboot的配置,对于非springboot配置则无能为力。

pom.xml


    <profiles>
        <!--本地环境-->
        <profile>
            <id>local</id>
            <properties>
                <profiles.active>local</profiles.active>
            </properties>
            <activation>
                <!--设置默认的环境-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!--福建环境-->
        <profile>
            <id>fj</id>
            <properties>
                <profiles.active>fj</profiles.active>
            </properties>
        </profile>
        <!--福建容器环境-->
        <profile>
            <id>fj-contioner</id>
            <properties>
                <profiles.active>fj-contioner</profiles.active>
            </properties>
        </profile>
        <!--国华环境-->
        <profile>
            <id>gh</id>
            <properties>
                <profiles.active>gh</profiles.active>
            </properties>
        </profile>
        <!--演示环境-->
        <profile>
            <id>ys</id>
            <properties>
                <profiles.active>ys</profiles.active>
            </properties>
        </profile>
        <!--湖南环境-->
        <profile>
            <id>hn</id>
            <properties>
                <profiles.active>hn</profiles.active>
            </properties>
        </profile>
        <!--冀北环境-->
        <profile>
            <id>jb</id>
            <properties>
                <profiles.active>jb</profiles.active>
            </properties>
        </profile>
        <!--辽宁环境-->
        <profile>
            <id>ln</id>
            <properties>
                <profiles.active>ln</profiles.active>
            </properties>
        </profile>
        <!--四川环境-->
        <profile>
            <id>sc</id>
            <properties>
                <profiles.active>sc</profiles.active>
            </properties>
        </profile>
        <!--四川现货生成环境-->
        <profile>
            <id>scxh</id>
            <properties>
                <profiles.active>scxh</profiles.active>
            </properties>
        </profile>
        <!--北京现货测试环境-->
        <profile>
            <id>bjxh</id>
            <properties>
                <profiles.active>bjxh</profiles.active>
            </properties>
        </profile>
        <!--四川现货测试环境-->
        <profile>
            <id>scxhtest</id>
            <properties>
                <profiles.active>scxhtest</profiles.active>
            </properties>
        </profile>
        <!--上海环境-->
        <profile>
            <id>sh</id>
            <properties>
                <profiles.active>sh</profiles.active>
            </properties>
        </profile>
        <!--天津环境-->
        <profile>
            <id>tj</id>
            <properties>
                <profiles.active>tj</profiles.active>
            </properties>
        </profile>
        <!--外网环境-->
        <profile>
            <id>star</id>
            <properties>
                <profiles.active>star</profiles.active>
            </properties>
        </profile>
        <!--外网环境thrift-->
        <profile>
            <id>tf-star</id>
            <properties>
                <profiles.active>tf-star</profiles.active>
            </properties>
        </profile>
        <!--北京开发环境-->
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>200</id>
            <properties>
                <profiles.active>200</profiles.active>
            </properties>
        </profile>
    </profiles>
    <build>
       <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定 -->
                <includes>
                    <include>banner.txt</include>
                    <include>static/**</include>
                </includes>
                <excludes>
                    <exclude>jb/**</exclude>
                    <exclude>local/**</exclude>
                    <exclude>sc/**</exclude>
                    <exclude>star/**</exclude>
                    <exclude>tf-star/**</exclude>
                    <exclude>fj/**</exclude>
                    <exclude>gh/**</exclude>
                    <exclude>hn/**</exclude>
                    <exclude>sh/**</exclude>
                    <exclude>bjxh/**</exclude>
                    <exclude>scxhtest/**</exclude>
                    <exclude>scxh/**</exclude>
                    <exclude>tencent/**</exclude>
                    <exclude>tj/**</exclude>
                    <exclude>ys/**</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${profiles.active}</directory>
                <!--是否替换资源中的属性-->
                <filtering>true</filtering>
            </resource>
        </resources>
     </build>

resources目录结构如下:

├── banner.txt                公共配置
├── elastic-certificates.p12  公共配置
├── bjxh       北京现货配置
│   ├── application.yml
│   └── static
│       └── poseidon-admin-ui
│           └── js
│               └── setting.js
├── dev。      开发环境
│   ├── application.yml
│   └── static
│       └── poseidon-admin-ui
│           └── js
│               └── setting.js
├── fj        福建环境
│   ├── application.yml
│   ├── server.properties
│   └── static
│       └── poseidon-admin-ui
│           └── js
│               └── setting.js
├── hn        湖南环境
│   ├── application.yml
│   └── static
│       └── poseidon-admin-ui
│           └── js
│               └── setting.js
├── jb。       冀北环境
│   ├── application.yml
│   └── static
│       └── poseidon-admin-ui
│           └── js
│               └── setting.js
├── ln。       辽宁环境
│   ├── application.yml
│   └── static
│       └── poseidon-admin-ui
│           └── js
│               └── setting.js
├── sc         四川环境
│   ├── application.yml
│   └── static
│       └── poseidon-admin-ui
│           └── js
│               └── setting.js

通用jar包管理脚本

app.sh

通用管理脚本使用方式:

bin/app.sh start:启动

bin/app.sh stop:停止

bin/app.sh restart:重启

bin/app.sh status:查看应用状态

bin/app.sh log:查看应用日志

#!/bin/bash
##########################################################
##########################################################
#####           java通用管理脚本
##### 功能: 启动、停止、重启、查看状态、查看日志、帮助
##### 作者: star
##### 时间: 2021-01-24
#####
##### 其他说明:依赖jdk环境变量
#####         需提前安装jdk并配置好环境变量
##########################################################
##########################################################


#Java程序所在的目录(classes的上一级目录)
APP_HOME=$(dirname $(cd "$(dirname "$0")"; pwd))
cd ${APP_HOME}
#日志写入位置
LOG_HOME="${APP_HOME}/logs"
CONF_HOME="${APP_HOME}/conf"
# 日志文件的名称
LOG_NAME=app.log
#应用名
APP_NAME="zeus-cpp-netive-server"
#jar包名称,需要与项目启动jar包保持一致
JAR_NAME="zeus-cpp-netive-server-1.0.jar"
#拼凑完整的路径
APP="${APP_HOME}/${JAR_NAME}"
echo ${APP}

if [ ! -d $LOGS_DIR ]; then
   mkdir $LOGS_DIR
fi

#java虚拟机启动参数
# -Dfile.encoding=UTF8   设置编码为UTF8
# -Duser.timezone=GMT+08 设置时区为上海时区(默认的时区为UTC)
# -Xms1024m -Xmx1024m    设置程序运行的内存配置
JAVA_OPTS=" -Dfile.encoding=UTF8 -Duser.timezone=GMT+08  -Xms1024m -Xmx1024m -Djava.library.path=$CONF_HOME"
JAR=$APP
prog=$APP_NAME
psid=0

checkpid() {
   javaps=`jps -l | grep $APP`

   if [ -n "$javaps" ]; then
      psid=`echo $javaps | awk '{print $1}'`
   else
      psid=0
   fi
}
start() {
    checkpid
   if [ ! -d $LOG_HOME ]; then
      mkdir -p $LOG_HOME
   fi
   if [ $psid -ne 0 ]; then
      echo "================================"
      echo "warn: $APP_NAME already started! (pid=$psid)"
      echo "================================"
   else
      echo -n "Starting $APP_NAME ..."
      nohup java $JAVA_OPTS -jar $APP >$LOG_HOME/$LOG_NAME  2>&1 &
      checkpid
      if [ $psid -ne 0 ]; then
         echo "(pid=$psid) [OK]"
      else
         echo "[Failed]"
      fi
   fi
}

stop() {


     checkpid

   if [ $psid -ne 0 ]; then
      echo -n "Stopping $APP_NAME ...(pid=$psid) "
       kill -9 $psid
      if [ $? -eq 0 ]; then
         echo "[OK]"
      else
         echo "[Failed]"
      fi

      checkpid
      if [ $psid -ne 0 ]; then
         stop
      fi
   else
      echo "================================"
      echo "warn: $APP_NAME is not running"
      echo "================================"
   fi
}

restart() {
    checkpid

   if [ $psid -ne 0 ];  then
      echo "$APP_NAME is running! (pid=$psid)"
      stop
      start
   else
      echo "$APP_NAME is not running"
      start
   fi
}

status() {
    pid=$(ps -ef | grep $JAR | grep -v 'grep ' | awk '{print $2}')

    if [  -e $pid ];then
        echo -e $"$prog hasn't run\t\t[OK]"
    else
        echo -e $"$prog is running\t\t[OK]"
    fi
}

log() {
    tail -111f $LOG_HOME/$LOG_NAME
}

case "$1" in
    start)
        start
            ;;
    stop)
            stop
            ;;
    status)
            status
            ;;
    restart)
        restart
        ;;
    log)
        log
        ;;
    *)
    echo -e "Usage: $0 {start|stop|status|restart|log}"
        exit 2
esac

通用assembly配置

assembly.xml

<assembly>
    <id>bin</id>
    <formats>
        <!--打包类型为tar.gz-->
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <!-- jar包 -->
        <fileSet>
            <directory>target</directory>
            <includes>
                <include>*.jar</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <!-- 配置文件 -->
        <fileSet>
            <directory>target/conf</directory>
            <outputDirectory>/conf</outputDirectory>
        </fileSet>
        <!-- 启动脚本 -->
        <fileSet>
            <directory>bin</directory>
            <fileMode>0744</fileMode>
            <outputDirectory>/bin</outputDirectory>
        </fileSet>
    </fileSets>
    <!-- 依赖包 -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

项目目录结构

├── assembly.xml   assembly插件配置脚本
├── bin            程序管理脚本,打包时会打到发布包中
│   └── app.sh
├── doc
│   └── readme.md
├── pom.xml

相关文章

网友评论

      本文标题:java项目打包配置

      本文链接:https://www.haomeiwen.com/subject/wwvnhrtx.html