一尘不染

使用Maven和Jetty创建可执行jar

spring

我想使用码头来启动我的应用程序,因此我添加了下面提到的依赖项。当我运行主要方法Jetty成功启动时(我正在一个struts2 + spring3 + hibernate maven项目中,我也可以将其部署在tomcat中)

现在,我想从war包装pom创建一个可执行jar。所以我在pom中添加了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> 

主要方法

 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();

启动应用程序
我使用java -jar /home/myfolder/workspace/app/dca/target/app-dca-1.0-jar-with-dependencies.jar运行创建的jar

码头始于例外。

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)

有没有一种可能的方法可以使我使用所创造的战争开始跳船。它在/ WEB-INF / lib /文件夹中有jars,所有属性文件和xml文件都在/ WEB-INF / lib /中,我尝试进行战争

java -jar /home/myfolder/workspace/app/dca/target/app-dca-1.0.war

但它无法找到主要类别。

创建可执行的war check sumit的答案时有效

我在这个问题上遇到了例外。当我将码头版本替换为7.6.7.v20120910时,它起作用了

我不知道为什么它不能与码头8.1.10.v20130312一起使用


阅读 447

收藏
2020-04-20

共2个答案

一尘不染

相关代码是

    ProtectionDomain domain = Main.class.getProtectionDomain();
    URL location = domain.getCodeSource().getLocation();
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setWar(location.toExternalForm());
    server.setHandler(webapp);

希望能有所帮助。

2020-04-20
一尘不染

使用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>

这将创建一个名为“ standalone.jar”的uberjar,然后你只需调用即可

java -jar standalone.jar

这将在http:// localhost:8080 / standalone上启动应用程序

选择备用端口很容易

java -jar standalone.jar -httpPort=7070
2020-04-20