Skip to content

Commit a3d5ac6

Browse files
committed
add maven plugin and empty example
1 parent 2af7feb commit a3d5ac6

File tree

9 files changed

+339
-0
lines changed

9 files changed

+339
-0
lines changed

all-package/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<module>../open-exp-code</module>
1515
<module>../spring-adapter</module>
1616
<module>../example</module>
17+
<module>../open-exp-code/auto-install-exp-maven-plugin</module>
1718
</modules>
1819

1920
</project>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>cn.think.in.java</groupId>
8+
<artifactId>example</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>example-plugin-empty</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>8</maven.compiler.source>
16+
<maven.compiler.target>8</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencyManagement>
21+
<dependencies>
22+
<dependency>
23+
<groupId>cn.think.in.java</groupId>
24+
<artifactId>exp-one-bom</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
<type>pom</type>
27+
<scope>import</scope>
28+
</dependency>
29+
</dependencies>
30+
</dependencyManagement>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>cn.think.in.java</groupId>
35+
<artifactId>open-exp-plugin-depend</artifactId>
36+
<version>1.0-SNAPSHOT</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>cn.think.in.java</groupId>
41+
<artifactId>example-extension-define</artifactId>
42+
<version>1.0-SNAPSHOT</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-web</artifactId>
48+
<version>2.3.12.RELEASE</version>
49+
</dependency>
50+
</dependencies>
51+
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>org.apache.maven.plugins</groupId>
57+
<artifactId>maven-shade-plugin</artifactId>
58+
<version>3.4.1</version>
59+
<configuration>
60+
<artifactSet>
61+
<includes>
62+
<include>cn.think.in.java:open-exp-plugin-depend</include>
63+
<include>cn.think.in.java:example-extension-define</include>
64+
</includes>
65+
</artifactSet>
66+
</configuration>
67+
<executions>
68+
<execution>
69+
<phase>package</phase>
70+
<goals>
71+
<goal>shade</goal>
72+
</goals>
73+
</execution>
74+
</executions>
75+
</plugin>
76+
77+
<plugin>
78+
<artifactId>maven-antrun-plugin</artifactId>
79+
<executions>
80+
<execution>
81+
<id>copy</id>
82+
<phase>package</phase>
83+
<configuration>
84+
<tasks>
85+
86+
<copy todir="../../exp-plugins">
87+
<!-- project.build.directory表示各个模块的target目录 -->
88+
<fileset dir="${project.build.directory}">
89+
<!-- 需要复制的jar包文件名称 -->
90+
<include name="${project.artifactId}-${project.version}.jar"/>
91+
</fileset>
92+
</copy>
93+
</tasks>
94+
</configuration>
95+
<goals>
96+
<goal>run</goal>
97+
</goals>
98+
</execution>
99+
</executions>
100+
</plugin>
101+
102+
<plugin>
103+
<!-- exp 插件辅助工具, 每次打包时, 都会将当前 jar 包上传到目标服务器,
104+
且: 如果目标服务器已经有了该插件, 则会卸载插件后再上传.
105+
-->
106+
<groupId>cn.think.in.java</groupId>
107+
<artifactId>auto-install-exp-maven-plugin</artifactId>
108+
<version>1.0-SNAPSHOT</version>
109+
<configuration>
110+
<jarName>${project.artifactId}-${project.version}.jar</jarName>
111+
<!-- 替换为你的 k8s 服务器地址 -->
112+
<uploadUrl>http://localhost:8888/uploadAndInstall</uploadUrl>
113+
<!-- 替换为你的 k8s 服务器地址 -->
114+
<uninstallUrl>http://localhost:8888/uninstall</uninstallUrl>
115+
<skipUpload>true</skipUpload>
116+
</configuration>
117+
<executions>
118+
<execution>
119+
<goals>
120+
<goal>upload</goal>
121+
</goals>
122+
</execution>
123+
</executions>
124+
</plugin>
125+
126+
127+
</plugins>
128+
</build>
129+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.think.in.java.open.exp.example.empty;
2+
3+
import cn.think.in.java.open.exp.client.ConfigSupport;
4+
import cn.think.in.java.open.exp.plugin.depend.AbstractBoot;
5+
6+
/**
7+
* @version 1.0
8+
* @Author cxs
9+
* @Description
10+
* @date 2023/8/9
11+
**/
12+
public class Boot extends AbstractBoot {
13+
14+
public static ConfigSupport configSupport = new ConfigSupport("empty", null);
15+
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.think.in.java.open.exp.example.empty;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
import static cn.think.in.java.open.exp.example.empty.Boot.configSupport;
7+
8+
/**
9+
* @version 1.0
10+
**/
11+
@RestController
12+
@RequestMapping("/hello")
13+
public class MyController {
14+
15+
@RequestMapping("/empty")
16+
public String hello() {
17+
return configSupport.getProperty();
18+
}
19+
}

example/example-plugin-empty/src/main/resources/extension.properties

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
plugin.boot.class=cn.think.in.java.open.exp.example.empty.Boot
2+
plugin.code=example.plugin.empty
3+
plugin.desc=this a plugin a empty demo
4+
plugin.version=1.0.0
5+
plugin.ext=null

example/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<module>example-springboot2</module>
1616
<module>example-springboot1</module>
1717
<module>example-extension-define</module>
18+
<module>example-plugin-empty</module>
1819
<module>example-plugin-a-v1</module>
1920
<module>example-plugin-a-v2</module>
2021
<module>example-plugin-b-v1</module>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>cn.think.in.java</groupId>
8+
<artifactId>auto-install-exp-maven-plugin</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>maven-plugin</packaging>
11+
12+
<properties>
13+
<maven.compiler.source>8</maven.compiler.source>
14+
<maven.compiler.target>8</maven.compiler.target>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.apache.maven</groupId>
21+
<artifactId>maven-core</artifactId>
22+
<version>2.2.1</version>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.apache.maven.plugin-tools</groupId>
27+
<artifactId>maven-plugin-annotations</artifactId>
28+
<version>3.6.4</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.apache.httpcomponents</groupId>
33+
<artifactId>httpmime</artifactId>
34+
<version>4.5.14</version>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-plugin-plugin</artifactId>
43+
<version>3.6.0</version>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</project>
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package cn.think.in.java.auto.install.maven.plugin;
2+
3+
import org.apache.http.HttpEntity;
4+
import org.apache.http.client.methods.CloseableHttpResponse;
5+
import org.apache.http.client.methods.HttpGet;
6+
import org.apache.http.client.methods.HttpPost;
7+
import org.apache.http.entity.mime.MultipartEntityBuilder;
8+
import org.apache.http.impl.client.CloseableHttpClient;
9+
import org.apache.http.impl.client.HttpClients;
10+
import org.apache.http.util.EntityUtils;
11+
import org.apache.maven.plugin.AbstractMojo;
12+
import org.apache.maven.plugin.MojoExecutionException;
13+
import org.apache.maven.plugins.annotations.LifecyclePhase;
14+
import org.apache.maven.plugins.annotations.Mojo;
15+
import org.apache.maven.plugins.annotations.Parameter;
16+
import org.codehaus.plexus.util.StringUtils;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.net.MalformedURLException;
21+
import java.net.URL;
22+
import java.net.URLClassLoader;
23+
import java.util.Properties;
24+
25+
@Mojo(name = "upload", defaultPhase = LifecyclePhase.PACKAGE)
26+
public class UploadMojo extends AbstractMojo {
27+
28+
@Parameter(defaultValue = "${project.build.directory}", property = "outputDir", required = true)
29+
private File outputDirectory;
30+
31+
@Parameter(property = "jarName", required = true)
32+
private String jarName;
33+
34+
@Parameter(property = "uploadUrl", required = true)
35+
private String uploadUrl;
36+
37+
@Parameter(property = "uninstallUrl", required = false)
38+
private String uninstallUrl;
39+
40+
@Parameter(defaultValue = "false", property = "skipUpload", required = true)
41+
private boolean skipUpload;
42+
43+
public void execute() throws MojoExecutionException {
44+
if (skipUpload) {
45+
getLog().info("Skipping upload.");
46+
return;
47+
}
48+
49+
File jarFile = new File(outputDirectory, jarName);
50+
if (!jarFile.exists()) {
51+
throw new MojoExecutionException("Jar file does not exist: " + jarFile);
52+
}
53+
54+
URLClassLoader urlClassLoader = null;
55+
try {
56+
urlClassLoader = new URLClassLoader(new URL[]{jarFile.toURL()}, Thread.currentThread().getContextClassLoader());
57+
} catch (MalformedURLException e) {
58+
throw new RuntimeException(e);
59+
}
60+
URL resource = urlClassLoader.findResource("pluginInfo.properties");
61+
URL resource2 = urlClassLoader.findResource("eep.config");
62+
Properties properties = new Properties();
63+
try {
64+
properties.load(resource.openStream());
65+
properties.load(resource2.openStream());
66+
} catch (IOException e) {
67+
throw new RuntimeException(e);
68+
}
69+
70+
Object o = properties.get("esign.plugin.code");
71+
Object o2 = properties.get("esign.plugin.version");
72+
73+
String id = o + "_" + o2;
74+
75+
getLog().info("插件 id = " + id);
76+
tryUninstall(id);
77+
78+
uploadFile(jarFile);
79+
}
80+
81+
void tryUninstall(String pluginId) throws MojoExecutionException {
82+
if (StringUtils.isEmpty(uninstallUrl)) {
83+
return;
84+
}
85+
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
86+
HttpGet uploadFile = new HttpGet(uninstallUrl + "?pluginid=" + pluginId);
87+
88+
CloseableHttpResponse response = httpClient.execute(uploadFile);
89+
HttpEntity responseEntity = response.getEntity();
90+
91+
getLog().info("卸载结束. Server response: " + EntityUtils.toString(responseEntity));
92+
} catch (Exception e) {
93+
throw new MojoExecutionException("Error uploading file", e);
94+
}
95+
96+
}
97+
98+
private void uploadFile(File file) throws MojoExecutionException {
99+
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
100+
HttpPost uploadFile = new HttpPost(uploadUrl);
101+
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
102+
builder.addBinaryBody("file", file);
103+
HttpEntity multipart = builder.build();
104+
uploadFile.setEntity(multipart);
105+
106+
CloseableHttpResponse response = httpClient.execute(uploadFile);
107+
HttpEntity responseEntity = response.getEntity();
108+
109+
int statusCode = response.getStatusLine().getStatusCode();
110+
if (statusCode != 200) {
111+
String reasonPhrase = response.getStatusLine().getReasonPhrase();
112+
throw new MojoExecutionException("Failed to upload file. Server returned status code: " + statusCode
113+
+ ", reasonPhrase = " + reasonPhrase);
114+
}
115+
116+
getLog().info("File uploaded successfully. Server response: " + EntityUtils.toString(responseEntity));
117+
} catch (Exception e) {
118+
throw new MojoExecutionException("Error uploading file", e);
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)