一、Maven打包项目
-
添加依赖,打包插件
<?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.ysxu</groupId> <artifactId>Hello</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.26</version> </dependency> <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <!--注意,此处必须是main()方法对应类的完整路径 --> <mainClass>Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
-
编写Main.java
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Main { private static final Logger logger = LoggerFactory.getLogger(Main.class); public static void main(String[] args) throws InterruptedException { while (true){ logger.info("Hello, Linux"); Thread.sleep(3000); } } }
-
编写logback.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%red(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n)</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> <charset>utf-8</charset> </encoder> <!-- <file>log/output.log</file>--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>log/%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <!-- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">--> <!-- <MaxFileSize>1MB</MaxFileSize>--> <!-- </triggeringPolicy>--> </appender> <root level="INFO"> <appender-ref ref="CONSOLE" /> <appender-ref ref="FILE" /> </root> </configuration>
-
打开Maven窗口,依次执行clean--->compile--->package
-
生成的jar文件在target目录下
二、将jar文件作为Linux的服务运行
-
复制jar文件到Linux
-
在与jar文件同一目录下编写start.sh,stop.sh
start.sh
#!/bin/bash export JAVA_HOME=/usr/local/java/jdk1.8.0_221 export JRE=/usr/local/java/jdk1.8.0_221/jre export CLASSPATH=$JAVA_HOME/lib:$JRE/lib:. export PATH=$PATH:$JAVA_HOME/bin/:$JRE/bin export PATH=$JAVA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin nohup java -jar /root/project/Hello-1.0-SNAPSHOT.jar >>/dev/null 2>&1 & echo $! > /var/run/Hello-1.0-SNAPSHOT.pid
stop.sh
#!/bin/bash PID=$(cat /var/run/Hello-1.0-SNAPSHOT.pid) kill -9 $PID
-
添加hello.service
(/usr/lib/systemd/system/hello.service)
[Unit] Description=hello server Documentation= Before= After= Wants= #Request= [Service] Type=forking WorkingDirectory=/root/project PermissionsStartOnly=true ExecStart=/root/project/start.sh KillMode=process ExecStop=/root/project/stop.sh PrivateTmp=true Restart=always RestartSec=2s LimitCORE=infinity [Install] WantedBy=multi-user.target
-
使用命令管理服务
systemctl start xxx systemctl stop xxx systemctl restart xxx systemctl enable xxx
三、遇到问题
-
Failed at step EXEC spawning /usr/local/yealink/ptms/ptms-start.sh: Permission denied
原因是没有执行sh文件的权限,这里通过chmod 777 ptms-start.sh给相应的文件赋予最高权限即可。
-
Failed at step EXEC spawning /usr/local/yealink/ptms/ptms.sh: No such file or directory
遇到sh文件找不到,但是确实路径又没错的情况,可以试着直接./ptms-start.sh执行sh文件,看看会不会报错:bad interpreter: No such file or directory,如果会基本就是这个sh文件格式的问题。
原因在于在window下编辑shell文件,换行符是不一样的,会产生不可见字符。我确实有把sh文件在window系统打开编辑过,此时在传到Linux系统上格式就会发生变化,在Linux上本来应该是unix的,但发现却是dos格式的。解决办法就是在linux上修改shell文件格式为unix即可。
四、参考