本项目是将restful项目打包成可执行的war包,在docker中执行
环境介绍:
docker 1.10.3
jetty 8
jersey 1.19
项目代码:
https://git.coding.net/firewa...
关键配置:
1. pom.xml配置
${project.artifactId}
org.mortbay.jetty
jetty-maven-plugin
${jetty.version}
/
org.apache.maven.plugins
maven-compiler-plugin
2.5.1
1.7
org.apache.maven.plugins
maven-source-plugin
2.2
attach-sources
jar
org.apache.maven.plugins
maven-dependency-plugin
2.5.1
jetty-classpath
prepare-package
unpack-dependencies
org.eclipse.jetty,org.mortbay.jetty,javax.servlet,org.glassfish.web
servlet-api-3.0,jsp-api,jsp-impl,jstl
${project.build.directory}/${project.artifactId}
org.apache.maven.plugins
maven-antrun-plugin
1.7
main-class-placement
prepare-package
run
org.apache.maven.plugins
maven-war-plugin
2.2
false
Launcher
docs/**
docs/**
2. 启动类
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.Properties;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
publicfinalclassLauncher{
publicstaticvoidmain(String[] args)throws Exception {
ProtectionDomain domain = Launcher.class.getProtectionDomain();
URL location = domain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(location.toExternalForm());
//as enumerated from http://jira.codehaus.org/browse/JETTY-1256
String[] configurations = new String[]{
"org.eclipse.jetty.webapp.WebInfConfiguration"
,"org.eclipse.jetty.webapp.WebXmlConfiguration"
,"org.eclipse.jetty.webapp.MetaInfConfiguration"
,"org.eclipse.jetty.webapp.FragmentConfiguration"
,"org.eclipse.jetty.plus.webapp.EnvConfiguration"
//,"org.eclipse.jetty.plus.webapp.Configuration"
,"org.eclipse.jetty.annotations.AnnotationConfiguration"
,"org.eclipse.jetty.webapp.JettyWebXmlConfiguration"
//,"org.eclipse.jetty.annotations.ContainerInitializerConfiguration"
};
webapp.setAttribute("org.eclipse.jetty.webapp.configuration", configurations);
webapp.setConfigurationClasses(configurations);
int port = 8080;
try{
//NOTE: default port in CONFIGPATH file is 8383
port = Integer.parseInt( load(new File(System.getProperty("CONFIGPATH"))).getProperty("jetty.port"));
}catch(Exception e){
e.printStackTrace();
System.out.println("ERROR: Invalid jetty.port value in configuration file.");
}
Server server = new Server(port);
server.setHandler(webapp);
server.start();
server.join();
}
privatestatic Properties load(File propsFile)throws IOException {
Properties props = new Properties();
FileInputStream fis = null;
try{
fis = new FileInputStream(propsFile);
props.load(fis);
}finally{
try{
fis.close();
}catch(Exception e){
}
}
return props;
}
}
3. web.xml配置
Rest_Servlet
com.sun.jersey.spi.container.servlet.ServletContainer
com.sun.jersey.config.property.packages
cn.firewarm.rest
1
Rest_Servlet
/test/*
CORS
com.thetransactioncompany.cors.CORSFilter
cors.allowOrigin
*
cors.supportedMethods
GET, POST, HEAD, PUT, DELETE,OPTIONS
cors.supportedHeaders
Accept, Origin, X-Requested-With, Content-Type, Last-Modified,Authorization
cors.exposedHeaders
Set-Cookie
cors.supportsCredentials
true
CORS
/*
Archetype Created Web Application
4. 配置文件conf.properties
jetty.port=8080
database.url=
database.driver_class=
database.user=
database.password=
hibernate.show_sql=false
5. Dockerfile配置
FROM isuper/java-oracle:jre_7
MAINTAINER Liuyg
# Expose the API port
EXPOSE 8080
ADD target target
RUN set -xchmod775 /target/*.sh
# Run the JAR
CMD java -DCONFIGPATH=./target/conf.properties -jar /target/docker-jetty-jersey1.x.war
6. jenkins配置,请参考我的其他文章:
jenkins构建Docker 镜像(基于Jenkins的Docker镜像及Jenkins插件):http://blog.csdn.net/gsying14...
by 刘迎光@萤火虫工作室
OpenBI交流群:495266201
MicroService 微服务交流群:217722918
mail: liuyg#liuyingguang.cn
博主首页(==防止爬虫==):http://blog.liuyingguang.cn
OpenBI问答社区:http://www.openbi.tk