您的位置:首页 > 其它

mvn配置,启动脚本,及遇到的常见问题

2018-02-05 10:43 363 查看
介绍mvn配置,启动脚本,及遇到的常见问题

这两种方式都可以放在子项目下进行

1打包成一个jar

用maven-shade-plugin 会把配置文件打到一起,如果需要改配置文件很不方便,适于配置文件动态读取zk的情况

1.1 代码

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>

<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>

<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.hnmzhc.daemon.Main</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

1.2启动命令

如:java -jar -Xmx512m -Xms256m fyt-task-1.0-SNAPSHOT.jar

2精细化打包

用maven-dependency-plugin,maven-resources-plugin,maven-jar-plugin ,可以指定哪些配置文件不放入打包中

2.1 mavn配置

<!-- 把依赖的jar包拷到lib目录下 -->
<build>
<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>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<!-- 以utf-8编码拷贝配置文件,拷贝过程中是可以做变量替换的,也就是说你的配置文件可以是个模板,里面的${}所包含的内容是可以拷贝过程中替换的 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<outputDirectory>${project.build.directory}</outputDirectory><!-- 把配置文件拷到和jar包同一个路径下 -->
<resources>
<resource>
<directory>src/main/resources/</directory>
<includes>
<include>beans.xml</include>
<include>jdbc.properties</include>
<include>logback.xml</include>
<include>startup.sh</include>
</includes>
<filtering>true</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>lib</classifier>
<excludes>
<exclude>beans.xml</exclude>
<exclude>jdbc.properties</exclude>
<exclude>logback.xml</exclude>
<exclude>*.bat</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

2.2 启动脚本

java -Dfile.encoding=UTF-8 -classpath "lib/:.:" 包名.含mian的类名

复杂启动脚本

#kconfig: - 95 15
# description: exce导入程序 start/stop/status script

#Location of JAVA_HOME (bin files)
export JAVA_HOME=/data/installed/jdk1.8.0_112

#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH

#需要启动的Java主程序(main方法类)
PROGRAM_NAME="com.hnmzhc.daemon.Main"

#USAGE is the message if this script is called without any options
USAGE="Usage: $0 {start,stop,status,restart}"

#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20

tomcat_pid() {
echo `ps -ef | grep $PROGRAM_NAME | grep -v grep | tr -s " "|cut -d" " -f2`
}

start() {
pid=$(tomcat_pid)
if [ -n "$pid" ];then
echo -e "$PROGRAM_NAME is already running (pid: $pid)"
else
echo -e "Starting $PROGRAM_NAME"
nohup java -Dfile.encoding=UTF-8 -classpat
a7e4
h "lib/*:.:*" com.hnmzhc.daemon.Main   > /dev/null 2>&1  &
status
fi
return 0
}

status(){
pid=$(tomcat_pid)
if [ -n "$pid" ];then
echo -e "$PROGRAM_NAME is running with pid: $pid"
else
echo -e "$PROGRAM_NAME  is not running"
fi
}

stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ];then
echo -e "Stoping $PROGRAM_NAME"
#  shutdown.sh

#let kwait=$SHUTDOWN_WAIT  count=0;
#until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
#do
#  echo -n -e "waiting for processes to exit";
#  sleep 1
#  let count=$count+1;
#done

if [ $count -gt $kwait ];then
echo -n -e "killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
kill -9 $pid
fi
else
echo -e "spider  is not running"
fi

return 0
}

user_exists(){
if id -u $1 >/dev/null 2>&1; then
echo "1"
else
echo "0"
fi
}

case $1 in
start)
start
;;

stop)
stop
;;

restart)
stop
start
;;

status)
status
;;

*)
echo -e $USAGE
;;
esac
exit 0
 http://blog.csdn.net/u012152619/article/details/51485297 这个配置文件写的不错,可以参考

2.3 常见问题

打包完毕会有两个文件xxx.jar和xxx-lib.jar,前者还是包含配置文件,后者不包含,不要部署前者,两者会互相干扰
注意1 windows平台路径分隔符不是冒号是分好
注意2 文件保存为utf-8形式
注意3 windwo平台的换行符(\r\n)与linux(\n)平台不一致,需要在IDEA中转换一下,否则脚本可能会有问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mvn 配置