从exec开始:java和启动使用maven-assembly-plugin创建的包有什么区别?

时间:2014-03-21 22:38:54

标签: maven jersey grizzly

我使用神器jersey-quickstart-grizzly2在Jersey user guide之后创建了我的第一个Web应用程序。一切似乎按预期工作:通过使用maven(mvn java:exec)启动我的项目或从eclipse启动我可以从C ++,Javascript等调用我的REST API ...

现在我想在另一个平台上运行这个Web应用程序,所以我使用maven-assembly-plugin来创建一个包含运行相同应用程序所需的内容的jar。我使用以下命令创建包:

mvn clean compile assembly:single

我的jar被编译了。如果我尝试运行它:

java -jar target/myjar.jar
它似乎开始得恰到好处。如果我从任何客户端调用我的REST API,我会看到指示我的方法按预期调用的日志,但响应始终为500.这是例如调用的方法:

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<MTTask> getTasks() {
   MTLog.info("Processing GET request.");
   return new LinkedList<MTTask>();
}

这是卷曲的输出:

$ curl -X GET -v http://localhost:8080/myapp/tasks
* Adding handle: conn: 0x11d4c30
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x11d4c30) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /myapp/tasks HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 500 Request failed.
< Content-Type: text/html;charset=ISO-8859-1
< Date: Fri, 21 Mar 2014 22:31:11 GMT
< Connection: close
< Content-Length: 1031
< 
<html><head><title>Grizzly 2.3.8</title><style><!--div.header {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#003300;font-size:22px;-moz-border-radius-topleft: 10px;border-top-left-radius: 10px;-moz-border-radius-topright: 10px;border-top-right-radius: 10px;padding-left: 5px}div.body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:#FFFFCC;font-size:16px;padding-top:10px;padding-bottom:10px;padding-left:10px}div.footer {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#666633;font-size:14px;-moz-border-radius-bottomleft: 10px;border-bottom-left-radius: 10px;-moz-border-radius-bottomright: 10px;border-bottom-right-radius: 10px;padding-left: 5px}BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}B {font-family:Tahoma,Arial,sans-serif;color:black;}A {color : black;}HR {color : #999966;}--></style> </head><body><div class="header">Request failed.</div><div class="body">Request failed.</div><div class="footer">Grizzly 2.3.8</div></body>* Closing connection 0
</html>

如果我在同一个位置和同一台机器上使用mvn java:exec运行相同的代码,这就是输出:

$ curl -X GET -v http://localhost:8080/myapp/tasks
* Adding handle: conn: 0x17a2c30
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x17a2c30) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /myapp/tasks HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Fri, 21 Mar 2014 22:33:23 GMT
< Content-Length: 2
< 
* Connection #0 to host localhost left intact
[]

正如所料。所以我的问题是:有什么区别?我可以以某种简单快捷的方式以某种方式创建该jar以在我的服务器上部署吗?

编辑:这是我的pom文件的构建部分:

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <inherited>true</inherited>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <goals>
                    <goal>java</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <mainClass>com.luke.Main</mainClass>
        </configuration>
    </plugin>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>com.luke.Main</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!-- bind to the packaging phase -->
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>

1 个答案:

答案 0 :(得分:0)

只需开始指定类路径即可解决问题:

java -cp target/myjar.jar myMainClass