feat:增加launcher-maven-plugin

This commit is contained in:
曾文豪
2023-02-01 10:10:43 +08:00
parent d006788c90
commit 7e94ea54a8
5 changed files with 146 additions and 9 deletions

23
pom.xml
View File

@@ -28,6 +28,7 @@
<module>springboot-message</module>
<module>springboot-encrypt</module>
<module>springboot-annotation</module>
<module>springboot-launcher</module>
</modules>
<properties>
@@ -108,6 +109,12 @@
<version>0.1.9</version>
</dependency>
<dependency>
<groupId>com.tiesheng</groupId>
<artifactId>springboot-launcher</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
@@ -126,6 +133,17 @@
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.tiesheng</groupId>
<artifactId>launcher-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>initScript</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
@@ -134,10 +152,7 @@
<includeSystemScope>true</includeSystemScope>
<!-- 打包后的jar可直接执行 -->
<executable>true</executable>
<embeddedLaunchScriptProperties>
<logFolder>/dev</logFolder>
<logFilename>null</logFilename>
</embeddedLaunchScriptProperties>
<embeddedLaunchScript>${basedir}/target/launchScript</embeddedLaunchScript>
</configuration>
</plugin>
</plugins>

View File

@@ -37,12 +37,13 @@
<build>
<plugins>
<plugin>
<groupId>com.tiesheng</groupId>
<artifactId>launcher-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<embeddedLaunchScript>../script/launchScript</embeddedLaunchScript>
</configuration>
</plugin>
</plugins>
</build>

View File

@@ -0,0 +1,32 @@
<?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.tiesheng</groupId>
<artifactId>launcher-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>0.0.1</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5.2</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,72 @@
package com.tiesheng.launcher;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.io.*;
import java.nio.charset.StandardCharsets;
/**
* 指定调用本类execute()方法的目标
**/
@Mojo(name = "initScript", defaultPhase = LifecyclePhase.PREPARE_PACKAGE)
public class CopyLauncherScriptMojo extends AbstractMojo {
@Parameter(defaultValue = "${basedir}/target", required = true)
private File outputDirectory;
public void setOutputDirectory(File outputDirectory) {
this.outputDirectory = outputDirectory;
}
@Override
public void execute() throws MojoExecutionException {
try {
copy("launchScript");
} catch (IOException e) {
throw new MojoExecutionException("copy launchScript error");
}
}
/**
* 复制脚本
*
* @param filename
* @throws IOException
*/
private void copy(String filename) throws IOException {
InputStream inputStream = getClass().getResourceAsStream("/" + filename);
BufferedWriter writer = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
File target = new File(outputDirectory + "/" + filename);
target.setExecutable(true, false);
target.setWritable(true, false);
target.setReadable(true, false);
writer = new BufferedWriter(new FileWriter(target));
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
writer.write(line);
writer.newLine();
}
writer.flush();
} finally {
if (inputStream != null) {
inputStream.close();
}
if (writer != null) {
writer.close();
}
}
}
}

View File

@@ -105,7 +105,7 @@ await_file() {
}
# Determine the script mode
action="run"
action="config"
if [[ "$MODE" == "auto" && -n "$init_script" ]] || [[ "$MODE" == "service" ]]; then
action="$1"
shift
@@ -220,6 +220,21 @@ run() {
return "$result"
}
config() {
## 设置自启动
ln -s $jarfile /etc/init.d/$1
chkconfig --add $1
## 生成配置文件
working_dir=$(dirname "$jarfile")
conf_file="$working_dir/$configfile"
echo "LOG_FOLDER=/dev" > $conf_file
echo "LOG_FILENAME=null" >> $conf_file
echo "RUN_ARGS='--server.port=$2 --spring.profiles.active=$3'" >> $conf_file
echo "JAVA_OPTS='-Dfile.encoding=utf-8'" >> $conf_file
}
# Call the appropriate action function
case "$action" in
start)
@@ -232,8 +247,10 @@ status)
status "$@"; exit $?;;
run)
run "$@"; exit $?;;
config)
config "$1" "$2" "$3" ; exit $?;;
*)
echo "Usage: $0 {start|stop|restart|status|run}"; exit 1;
echo "Usage: $0 {start|stop|restart|status|run|config}"; exit 1;
esac
exit 0