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

介绍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 -classpath "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中转换一下,否则脚本可能会有问题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,mvn是Maven的缩写,是一个Java项目管理工具。它可以帮助我们自动化构建、测试和部署Java项目。而Node.js是一个基于Chrome V8引擎的JavaScript运行环境,用于开发服务器端应用程序。在使用Maven管理Java项目时,我们可能需要安装Node.js并配置其环境。 以下是在Maven中管理Node.js安装及环境配置的步骤: 1. 安装Node.js 可以从Node.js官网下载适合自己操作系统的安装包进行安装。安装完成后,可以在命令行中输入node -v来检查是否安装成功。 2. 配置环境变量 在Windows系统中,需要将Node.js的安装路径添加到系统环境变量中。具体步骤如下: - 右键点击“计算机”或者“此电脑”,选择“属性”; - 点击“高级系统设置”; - 点击“环境变量”; - 在“系统变量”中找到“Path”,双击进行编辑; - 在编辑窗口中添加Node.js的安装路径,多个路径之间用分号隔开; - 点击“确定”保存修改。 3. 在Maven中配置Node.js插件 在pom.xml文件中添加以下插件配置: ``` <build> <plugins> <plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> <version>1.7.6</version> <executions> <execution> <id>install node and npm</id> <goals> <goal>install-node-and-npm</goal> </goals> <configuration> <nodeVersion>v10.16.0</nodeVersion> <npmVersion>6.9.0</npmVersion> </configuration> </execution> <execution> <id>npm install</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>install</arguments> </configuration> </execution> <execution> <id>npm run build</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>run build</arguments> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 其中,nodeVersion和npmVersion分别指定了要安装的Node.js和npm的版本号。在执行Maven构建时,自动下载并安装指定版本的Node.js和npm,并执行npm install和npm run build命令。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值