本项目是将restful项目打包成可执行的war包,在docker中执行
环境介绍:
docker 1.10.3
jetty 8
jersey 1.19
项目代码:
https://git.coding.net/firewa...
关键配置:
1. pom.xml配置
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<systemProperties>
</systemProperties>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>jetty-classpath</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>org.eclipse.jetty,org.mortbay.jetty,javax.servlet,org.glassfish.web</includeGroupIds>
<excludeArtifactIds>servlet-api-3.0,jsp-api,jsp-impl,jstl</excludeArtifactIds>
<outputDirectory>
${project.build.directory}/${project.artifactId}
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>main-class-placement</id>
<phase>prepare-package</phase>
<configuration>
<tasks>
<echomessage="*** Moving launcher.class..." />
<movetodir="${project.build.directory}/${project.artifactId}/">
<filesetdir="${project.build.directory}/classes/">
<includename="Launcher.class" />
</fileset>
</move>
<echomessage="*** Moving launcher.class done." />
<echomessage="*** Removing *.SF, *.RSA in META-INF ..." />
<delete>
<fileset
dir="${project.build.directory}/${project.artifactId}/META-INF/"
includes="*.SF,*.RSA" />
</delete>
<echomessage="*** Removing *.SF, *.RSA in META-INF done." />
<echomessage="*** Copying logback-test.xml..." />
<copytodir="${project.build.directory}/${project.artifactId}/">
<filesetdir="${project.build.directory}/../src/main/resources/">
<includename="logback-test.xml" />
</fileset>
</copy>
<echomessage="*** Copying logback-test.xml done." />
<echomessage="*** Copying conf.properties ..." />
<copytodir="${project.build.directory}/${project.artifactId}/../">
<filesetdir="${project.build.directory}/../src/main/resources/">
<includename="conf.properties" />
</fileset>
</copy>
<echomessage="*** Copying conf.properties done." />
<echomessage="*** Copying run.sh ..." />
<copytodir="${project.build.directory}/${project.artifactId}/../">
<filesetdir="${project.build.directory}/../src/main/resources/">
<includename="run.sh" />
</fileset>
</copy>
<echomessage="*** Copying run.sh done." />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<mainClass>Launcher</mainClass>
</manifest>
</archive>
<warSourceExcludes>docs/**</warSourceExcludes>
<packagingExcludes>docs/**</packagingExcludes>
</configuration>
</plugin>
<!-- <plugin> <groupId>org.codehaus.enunciate</groupId> <artifactId>maven-enunciate-plugin</artifactId>
<version>1.26.2</version> <executions> <execution> <goals> <goal>assemble</goal>
</goals> <configuration> <configFile>src/main/resources/enunciate.xml</configFile>
</configuration> </execution> </executions> </plugin> -->
</plugins>
</build>
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配置
<?xml version="1.0" encoding="UTF-8"?>
<web-appversion="3.0"xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>Rest_Servlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>cn.firewarm.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Rest_Servlet</servlet-name>
<!-- Redirect any calls to our jersey servlet -->
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
<!-- 添加解决跨域的代码配置,基于pom中有cors-filter的依赖 ,begin -->
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, DELETE,OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified,Authorization</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>Set-Cookie</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 添加解决跨域的代码配置,基于pom中有cors-filter的依赖 ,end -->
<display-name>Archetype Created Web Application</display-name>
</web-app>
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 <liuyg@liuyingguang.cn>
# 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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。