Преглед изворни кода

Add Main with embedded Tomcat to start project

Craig Walls пре 13 година
родитељ
комит
c64215d510
2 измењених фајлова са 39 додато и 3 уклоњено
  1. 7
    3
      start/build.gradle
  2. 32
    0
      start/src/main/java/hello/Main.java

+ 7
- 3
start/build.gradle Прегледај датотеку

1
 apply plugin: 'java'
1
 apply plugin: 'java'
2
-apply plugin: 'jetty'
3
 apply plugin: 'eclipse-wtp'
2
 apply plugin: 'eclipse-wtp'
4
 apply plugin: 'idea'
3
 apply plugin: 'idea'
4
+apply plugin: 'application'
5
+
6
+mainClassName = "hello.Main"
5
 
7
 
6
 repositories { mavenCentral() }
8
 repositories { mavenCentral() }
7
 
9
 
8
 dependencies {
10
 dependencies {
9
 	compile "org.springframework:spring-webmvc:3.2.2.RELEASE"
11
 	compile "org.springframework:spring-webmvc:3.2.2.RELEASE"
10
-	compile "com.fasterxml.jackson.core:jackson-core:2.1.4"
11
-	providedCompile "javax.servlet:servlet-api:2.5"
12
+	compile "com.fasterxml.jackson.core:jackson-databind:2.1.4"
13
+	compile "org.apache.tomcat:tomcat-catalina:7.0.39"
14
+	compile "org.apache.tomcat.embed:tomcat-embed-core:7.0.39"
15
+	compile "org.apache.tomcat:tomcat-jasper:7.0.39"
12
 }
16
 }
13
 
17
 
14
 task wrapper(type: Wrapper) {
18
 task wrapper(type: Wrapper) {

+ 32
- 0
start/src/main/java/hello/Main.java Прегледај датотеку

1
+package hello;
2
+
3
+import java.util.HashSet;
4
+
5
+import org.apache.catalina.Context;
6
+import org.apache.catalina.core.AprLifecycleListener;
7
+import org.apache.catalina.core.StandardServer;
8
+import org.apache.catalina.startup.Tomcat;
9
+import org.springframework.web.SpringServletContainerInitializer;
10
+
11
+public class Main {
12
+
13
+	public static void main(String[] args) throws Exception {
14
+		Tomcat tomcat = new Tomcat();
15
+		tomcat.setPort(8080);
16
+		tomcat.setBaseDir(".");
17
+		tomcat.getHost().setAppBase("/");
18
+
19
+		// Add AprLifecycleListener
20
+		StandardServer server = (StandardServer)tomcat.getServer();
21
+		AprLifecycleListener listener = new AprLifecycleListener();
22
+		server.addLifecycleListener(listener);
23
+
24
+		Context context = tomcat.addWebapp("/", "/");
25
+		HashSet<Class<?>> classes = new HashSet<Class<?>>();
26
+		classes.add(HelloWorldWebAppInitializer.class);
27
+		context.addServletContainerInitializer(new SpringServletContainerInitializer(), classes);
28
+		tomcat.start();
29
+		tomcat.getServer().await();
30
+	}
31
+	
32
+}