Java 使用 maven 和 jetty 创建一个可执行的 jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20491407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create a executable jar using maven and jetty
提问by jos
I want to start my application using jetty, so I have added the dependency mentioned below. and when I run the main method Jetty starts successfully.(I am working on a struts2+spring3+ hibernate maven project, i am able to deploy it in tomcat too)
我想使用 jetty 启动我的应用程序,所以我添加了下面提到的依赖项。当我运行主要方法 Jetty 时,Jetty 成功启动。(我正在处理一个 struts2+spring3+ hibernate maven 项目,我也可以在 tomcat 中部署它)
Now I want to create a executable jar from a war packaging pom. So i added maven-assembly-pluginto my pom. (I tried with maven jar plug-in but it was not adding the dependencies)
现在我想从一个战争包装 pom 创建一个可执行的 jar。所以我在我的 pom.xml 文件中添加了maven-assembly-plugin。(我尝试使用 maven jar 插件,但它没有添加依赖项)
Sources
来源
plugins
插件
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<mainClass>com.dca.engine.StartDCA</mainClass>
</manifest>
</archive>
<packagingExcludes>WEB-INF/lib/jetty*.jar,WEB-INF/lib/org.apache.taglibs.standard.glassfish*.jar,WEB-INF/lib/org.apache.jasper.glassfish*.jar,WEB-INF/lib/org.eclipse.jdt.core*.jar,WEB-INF/lib/javax.servlet.jsp*.jar,WEB-INF/lib/javax.el*.jar</packagingExcludes>
<escapeString>\</escapeString>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.dca.engine.StartDCA</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Embedded Jetty
嵌入式码头
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.10.v20130312</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.10.v20130312</version>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>8.1.10.v20130312</version>
<!-- <scope>provided</scope> -->
</dependency>
main method
主要方法
Server server = new Server(8080);
System.setProperty("is_DCA", "YES");
WebAppContext webAppContext = new WebAppContext();
webAppContext.setResourceBase("/home/myfolder/workspace/app/dca/src/main/webapp");
webAppContext.setDescriptor("/home/myfolder/workspace/app/dca/src/main/webapp/WEB-INF/web.xml");
webAppContext.setContextPath("/app");
server.setHandler(webAppContext);
server.start();
server.join();
Starting the application
启动应用程序
I run the created jar with java -jar /home/myfolder/workspace/app/dca/target/app-dca-1.0-jar-with-dependencies.jar
我用 java -jar /home/myfolder/workspace/app/dca/target/app-dca-1.0-jar-with-dependencies.jar 运行创建的 jar
jetty starts with exception.
码头开始异常。
INFO 10-12 15:03:01,609 - jetty-8.y.z-SNAPSHOT
INFO 10-12 15:03:01,776 - Initializing Spring root WebApplicationContext
INFO 10-12 15:03:01,776 - Root WebApplicationContext: initialization started
INFO 10-12 15:03:01,843 - Refreshing Root WebApplicationContext: startup date [Tue Dec 10 15:03:01 IST 2013]; root of context hierarchy
INFO 10-12 15:03:01,885 - Loading XML bean definitions from class path resource [applicationContext-dca.xml]
ERROR 10-12 15:03:05,725 - Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/util]
Offending resource: class path resource [applicationContext-dca.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:316)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1420)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1413)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:184)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
Is there a possible way that i can start jetty using the war created. It has jars in /WEB-INF/lib/ folder, all properties file and xml files are in /WEB-INF/lib/ and I tried running the war
有没有一种可能的方法可以使用创建的战争启动码头。它在 /WEB-INF/lib/ 文件夹中有 jars,所有属性文件和 xml 文件都在 /WEB-INF/lib/ 中,我尝试运行战争
java -jar /home/myfolder/workspace/app/dca/target/app-dca-1.0.war
java -jar /home/myfolder/workspace/app/dca/target/app-dca-1.0.war
but it was not able to locate the main class.
但它无法找到主类。
worked when created executable war check sumit's answer
在创建可执行战争检查 sumit 的答案时工作
I was getting the exception as in this question. when I replaced jetty version to 7.6.7.v20120910 it worked
我在这个问题中得到了例外。当我将码头版本替换为 7.6.7.v20120910 时,它起作用了
i dont know why it didn't worked with jetty 8.1.10.v20130312
我不知道为什么它不适用于码头 8.1.10.v20130312
采纳答案by Sumit
Take a look at http://uguptablog.blogspot.com/2012/09/embedded-jetty-executable-war-with.html
看看http://uguptablog.blogspot.com/2012/09/embedded-jetty-executable-war-with.html
The relevant code is
相关代码是
ProtectionDomain domain = Main.class.getProtectionDomain();
URL location = domain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(location.toExternalForm());
server.setHandler(webapp);
hope that helps.
希望有帮助。
回答by Jean-Rémy Revy
The simpliest way I know (but never tried) is the winstone-maven-plugin, like Jenkins CI do (what a better example :D). It doesn't use Jetty (even I love this tool :D), but Winstone.
我知道(但从未尝试过)最简单的方法是winstone-maven-plugin,就像 Jenkins CI 那样(这是一个更好的例子:D)。它不使用 Jetty(即使我喜欢这个工具 :D),而是使用 Winstone。
Maven Plugin Usage
Maven 插件使用
<plugin>
<groupId>net.sf.alchim</groupId>
<artifactId>winstone-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>embed</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
Quite easy, isn't it ? :) It would generate a executable - standalone - jar : app-dca-1.0-standalone.jar
很容易,不是吗?:) 它会生成一个可执行文件——独立的——jar:app-dca-1.0-standalone.jar
Documentation
文档
- Winstone Servlet Container : http://winstone.sourceforge.net/
- Winstone Maven plugin : http://alchim.sourceforge.net/winstone-maven-plugin/usage.html
- A full example : http://www.jayway.com/2008/11/28/executable-war-with-winstone-maven-plugin/
- Winstone Servlet 容器:http: //winstone.sourceforge.net/
- Winstone Maven 插件:http: //alchim.sourceforge.net/winstone-maven-plugin/usage.html
- 一个完整的例子:http: //www.jayway.com/2008/11/28/executable-war-with-winstone-maven-plugin/
回答by Gili
Instead of hacking the contents of a WAR file, as the accepted answerseems to be doing, you can create an executable JAR file using the maven-shade-plugin. This has the following advantages:
您可以使用maven-shade-plugin创建一个可执行的 JAR 文件,而不是像接受的答案那样破解 WAR 文件的内容。这具有以下优点:
- The plugin contains "transformers" for merging the contents of META-INF/services, LICENSE and README files.
- You end up with a JAR file. WAR files were not designed to be executable (although it works).
- By unpacking all dependencies into the same JAR file, you are forced to remove duplicate classes and make the contents harder to reverse-engineer.
- 该插件包含用于合并 META-INF/services、LICENSE 和 README 文件内容的“转换器”。
- 您最终会得到一个 JAR 文件。WAR 文件并非设计为可执行的(尽管它可以工作)。
- 通过将所有依赖项解压到同一个 JAR 文件中,您将被迫删除重复的类并使内容更难逆向工程。
回答by JedA
If you are not tied to using Jetty but are open to using Tomcat instead, the following will work beautifully:
如果您不依赖于使用 Jetty,而是愿意使用 Tomcat,那么以下内容将非常有效:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<path>/standalone</path>
<enableNaming>false</enableNaming>
<finalName>standalone.jar</finalName>
<charset>utf-8</charset>
</configuration>
</execution>
</executions>
</plugin>
This will create an uberjar called "standalone.jar" that you can then run simply by calling
这将创建一个名为“standalone.jar”的 uberjar,然后您可以通过调用简单地运行它
java -jar standalone.jar
this will start the application on http://localhost:8080/standalone
这将在http://localhost:8080/standalone上启动应用程序
Selecting an alternate port is easy
选择备用端口很容易
java -jar standalone.jar -httpPort=7070
Thanks to Tomasz Nurkiewicz's blog entry for highlighting this http://www.nurkiewicz.com/2012/11/standalone-web-application-with.html
感谢 Tomasz Nurkiewicz 的博客文章强调了这个http://www.nurkiewicz.com/2012/11/standalone-web-application-with.html