mvn configuration, startup script, and common problems encountered

Keywords: Maven Apache Java encoding

Introduce mvn configuration, startup script, and common problems encountered

Both methods can be carried out under subprojects

1 package into a jar

Using Maven shade plugin will put the configuration files together. If you need to change the configuration file, it is inconvenient. It is suitable for dynamic reading zk of the configuration file

1.1 code

<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 start command

For example: java -jar -Xmx512m -Xms256m fyt-task-1.0-SNAPSHOT.jar

2 fine packaging

With Maven dependency plugin, Maven resources plugin, Maven jar plugin, you can specify which configuration files are not included in the package

2.1 mavn configuration


   <!-- DependentjarWrap up tolibDirectory -->
    <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>
            <!-- withutf-8Code and copy the configuration file. In the process of copying, variables can be replaced. That is to say, your configuration file can be a template${}The contents can be replaced in the copying process -->
            <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><!-- Copy configuration files to andjarPackage under the same path -->
                            <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>
            <!-- hitjarThe configuration file needs to be excluded when package -->
            <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 startup script

  • java -Dfile.encoding=UTF-8 -classpath "lib /:.:" package name. Class name with mian

  • Complex startup script

#kconfig: - 95 15   
# description: exceImport program 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  

#Required to startJavaMain program (mainMethod class)
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 

2.3 common problems

  • After packaging, there will be two files, xxx.jar and XXX lib.jar. The former still contains configuration files and the latter does not. Do not deploy the former. They will interfere with each other
  • Note that the 1 windows platform path separator is not a colon, but a good partition
  • Note 2 save the file as utf-8
  • Note that the newline character (\ r\n) of windwo platform is not consistent with that of Linux platform. You need to convert it in IDEA, otherwise the script may have problems

Posted by chauffeur on Wed, 15 Apr 2020 09:24:29 -0700