Skip to content

Commit 1009796

Browse files
committed
👌 更新嵌入式Tomcat启动类
1 parent 3cbf55c commit 1009796

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

codes/javatool/server/README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
88
**版本**
99

10-
- JDK:1.8
11-
- Tomcat:8.5.24
10+
* JDK:1.8
11+
* Tomcat:8.5.24
1212

1313
## Tomcat
1414

15-
### API 启动嵌入式 Tomcat
15+
### Windows 启动
1616

1717
执行 `io.github.dunwu.javatool.server.SimpleTomcatServer#main` 方法。
1818

@@ -24,6 +24,27 @@
2424

2525
由于插件很久没有更新(最新版本发布时间:2013-11-11),目前只能找到 Tomcat6 、Tomcat7 插件,所以弃用。
2626

27+
### 脚本启动
28+
29+
> 本项目添加了脚本启动范例。
30+
>
31+
> 脚本代码全在 [`scripts`](https://github.com/dunwu/java-stack/tree/master/scripts) 目录下。
32+
33+
* 初始化
34+
35+
```sh
36+
wget https://raw.githubusercontent.com/dunwu/java-stack/master/scripts/init.sh
37+
chmod 777 init.sh
38+
./init.sh
39+
```
40+
41+
* 发布
42+
43+
```
44+
cd /home/zp/source/java-stack/scripts
45+
./javatool-server-release.sh master develop
46+
```
47+
2748
## Jetty
2849

2950
待添加。。。
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.github.dunwu.javatool.server;
2+
3+
import java.util.Optional;
4+
5+
import org.apache.catalina.startup.Tomcat;
6+
7+
/**
8+
* 简单的嵌入式 Tomcat 启动类
9+
* 启动后可访问 http://localhost:8080/javatool-server/
10+
* @author Zhang Peng
11+
*/
12+
public class SimpleTomcatServer {
13+
private static final int PORT = 8080;
14+
private static final String CONTEXT_PATH = "/javatool-server";
15+
16+
public static void main(String[] args) throws Exception {
17+
// 设定 profile
18+
Optional<String> profile = Optional.ofNullable(System.getProperty("spring.profiles.active"));
19+
System.setProperty("spring.profiles.active", profile.orElse("develop"));
20+
21+
Tomcat tomcat = new Tomcat();
22+
tomcat.setPort(PORT);
23+
tomcat.getHost().setAppBase(".");
24+
tomcat.addWebapp(CONTEXT_PATH, getAbsolutePath() + "src/main/webapp");
25+
tomcat.start();
26+
tomcat.getServer().await();
27+
}
28+
29+
private static String getAbsolutePath() {
30+
String path = null;
31+
String folderPath = SimpleTomcatServer.class.getProtectionDomain().getCodeSource().getLocation().getPath()
32+
.substring(1);
33+
if (folderPath.indexOf("target") > 0) {
34+
path = folderPath.substring(0, folderPath.indexOf("target"));
35+
}
36+
return path;
37+
}
38+
}

0 commit comments

Comments
 (0)