pom.xml中的配置
<span style="white-space:pre"> </span><plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>cn.sh.ideal.imr.boot.Bootstrap</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个jar包的程序入口。
java代码
import java.io.FileNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Log4jConfigurer;
import com.google.common.util.concurrent.AbstractIdleService;
public class Bootstrap extends AbstractIdleService {
private static Logger log = LoggerFactory.getLogger(Bootstrap.class);
private ClassPathXmlApplicationContext context;
static {
try {
Log4jConfigurer.initLogging("classpath:conf/log4j.properties");
} catch (FileNotFoundException ex) {
System.err.println("Cannot Initialize log4j");
}
}
public static void main(String[] args) {
Bootstrap bootstrap = new Bootstrap();
bootstrap.startAsync();
try {
Object lock = new Object();
synchronized (lock) {
while (true) {
lock.wait();
}
}
} catch (InterruptedException ex) {
log.error("ignore interruption");
}
}
@Override
protected void startUp() throws Exception {
context = new ClassPathXmlApplicationContext(new String[]{"spring/spring.xml"});
context.start();
context.registerShutdownHook();
log.info(" service started successfully!");
}
@Override
protected void shutDown() throws Exception {
context.stop();
log.info(" service stop successfully!");
}
}
用maven打包后,部署发布,执行java -jar 项目名.jar命令,启动jar包
参考文章:
http://xinklabi.iteye.com/blog/2157591
Servlet 3 + Spring MVC零配置:去除所有xml
http://blog.youkuaiyun.com/xiao__gui/article/details/46803193
Google-Guava Concurrent包里的Service框架浅析
http://ifeve.com/google-guava-serviceexplained/
浅谈java 执行jar包中的main方法
http://www.jb51.net/article/92712.htm