2014-03-05 46 views
2

我剛開始編寫我的第一個Jax-RS webservice,並且從昨天開始,我嘗試瞭解如何使用maven構建項目以自動啓動測試服務器。如果我運行下面的命令,而不試圖執行一些測試,一切正常:啓動集成測試服務器失敗

mvn clean install 
export PORT=5000 
java -cp target/classes:"target/dependency/*" net.avedo.spozz.Spozz 

該服務,然後可在本地主機:5000 /業務/用戶。但是,如果我嘗試啓動服務器,並通過使用附加我在這個崗位年底pom.xml中自動運行測試,我得到一個錯誤:

testHasUser(net.avedo.spozz.models.UserTest): Connection refused 

相當於這段代碼:

HttpClient httpClient = new DefaultHttpClient(); 
HttpGet httpGet = new HttpGet("http://localhost:5000/services/users/1") ; 
response = httpClient.execute(httpGet) ; 

所以,我不確定這裏有什麼問題。我認爲服務器是在錯誤的uri下啓動的,但由於我沒有在測試服務器啓動的地方給出電話,所以我無法驗證這一點。我需要知道我需要在pom.xml文件中添加或更改什麼,以便運行測試服務器來運行項目的測試。由於它可以幫助,這是我的項目結構:

|-pom.xml 
    |-src 
    |---main 
    |-----java 
    |-------net 
    |---------avedo 
    |-----------spozz 
    |-------------Spozz.java 
    |-------------models 
    |---------------User.java 
    |-------------services 
    |---------------UserResource.java 
    |-----resources 
    |-----webapp 
    |-------index.html 
    |-------WEB-INF 
    |---------web.xml 
    |---test 
    |-----java 
    |-------net 
    |---------avedo 
    |-----------spozz 
    |-------------models 
    |---------------UserTest.java 
    |-----resources 
    |-target 
    |---classes 
    |-----net 
    |-------avedo 
    |---------spozz 
    |-----------Spozz.class 
    |-----------models 
    |-------------User.class 
    |-----------services 
    |-------------UserResource$1.class 
    |-------------UserResource$2.class 
    |-------------UserResource.class 
    |---test-classes 
    |-----net 
    |-------avedo 
    |---------spozz 
    |-----------models 
    |-------------UserTest.class 

我的舊的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 
    <groupId>net.avedo.spozz</groupId> 
    <artifactId>Spozz-Service</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>Spozz REST Webservice</name> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 
     <!-- Jetty --> 
     <dependency> 
      <groupId>org.eclipse.jetty</groupId> 
      <artifactId>jetty-servlet</artifactId> 
      <version>7.6.0.v20120127</version> 
     </dependency> 
     <dependency> 
      <groupId>org.eclipse.jetty</groupId> 
      <artifactId>jetty-webapp</artifactId> 
      <version>7.6.0.v20120127</version> 
     </dependency> 

     <!-- Jersey --> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-server</artifactId> 
      <version>1.8</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-json</artifactId> 
      <version>1.8</version> 
     </dependency> 

     <!-- jUnit --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.10</version> 
      <scope>test</scope> 
     </dependency> 

     <!-- Apache Commons --> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-io</artifactId> 
      <version>1.3.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.httpcomponents</groupId> 
      <artifactId>httpclient</artifactId> 
      <version>4.3.2</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.mortbay.jetty</groupId> 
       <artifactId>maven-jetty-plugin</artifactId> 
       <version>6.1.10</version> 
       <configuration> 
        <scanIntervalSeconds>10</scanIntervalSeconds> 
        <stopKey>foo</stopKey> 
        <stopPort>5000</stopPort> 
       </configuration> 
       <executions> 
        <execution> 
         <id>start-jetty</id> 
         <phase>pre-integration-test</phase> 
         <goals> 
          <goal>run</goal> 
         </goals> 
         <configuration> 
          <scanIntervalSeconds>0</scanIntervalSeconds> 
          <daemon>true</daemon> 
         </configuration> 
        </execution> 
        <execution> 
         <id>stop-jetty</id> 
         <phase>post-integration-test</phase> 
         <goals> 
          <goal>stop</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.4</version> 
       <executions> 
        <execution> 
         <id>copy-dependencies</id> 
         <phase>package</phase> 
         <goals><goal>copy-dependencies</goal></goals> 
        </execution> 
       </executions> 
      </plugin> 
    </plugins> 

     <pluginManagement> 
      <plugins> 

       <!-- M2Eclipse Compatibility --> 
       <plugin> 
        <groupId>org.eclipse.m2e</groupId> 
        <artifactId>lifecycle-mapping</artifactId> 
        <version>1.0.0</version> 
        <configuration> 
         <lifecycleMappingMetadata> 
          <pluginExecutions> 
           <pluginExecution> 
            <pluginExecutionFilter> 
             <groupId>org.apache.maven.plugins</groupId> 
             <artifactId>maven-dependency-plugin</artifactId> 
             <versionRange>[2.4,)</versionRange> 
             <goals> 
              <goal>copy-dependencies</goal> 
             </goals> 
            </pluginExecutionFilter> 
            <action> 
             <execute /> 
            </action> 
           </pluginExecution> 
          </pluginExecutions> 
         </lifecycleMappingMetadata> 
        </configuration> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
    </build> 
</project> 

\\ EDIT(1):

我更新了我的pom.xml到使用更新版本的碼頭,但現在我收到新的錯誤:

2014-03-06 11:51:29.126:WARN:oejuc.AbstractLifeCycle:FAILED [email protected]: 
java.lang.NoClassDefFoundError: org/eclipse/jetty/servlet/FilterMapping 

而且端口問題再次彈出:

Error binding monitor port 8080: java.net.BindException: Address already in use 
2014-03-06 11:51:24.909:INFO:oejs.Server:jetty-8.1.14.v20131031 
2014-03-06 11:51:25.245:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one. 
2014-03-06 11:51:26.839:WARN:oejsh.RequestLogHandler:!RequestLog 
2014-03-06 11:51:26.847:INFO:oejs.AbstractConnector:Started [email protected]:28080 

我更新的pom.xml

<project 
    xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 
    <groupId>net.avedo.spozz</groupId> 
    <artifactId>Spozz-Webservice</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <jetty.version>9.1.0.v20131115</jetty.version> 
     <jersey.version>1.8</jersey.version> 
     <junit.version>4.11</junit.version> 
     <apache.commons.version>1.3.2</apache.commons.version> 
     <apache.http.version>4.3.2</apache.http.version> 
     <jsp.version>2.5</jsp.version> 
     <maven.compiler.plugin.version>2.5.1</maven.compiler.plugin.version> 
     <sql.maven.plugin.version>1.5</sql.maven.plugin.version> 
     <postgresql.jdbc.version>9.1-901.jdbc4</postgresql.jdbc.version> 
    </properties> 

    <dependencies> 
     <!-- Jetty --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <version>3.1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.eclipse.jetty</groupId> 
      <artifactId>jetty-webapp</artifactId> 
      <version>${jetty.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.eclipse.jetty</groupId> 
      <artifactId>jetty-jsp</artifactId> 
      <version>${jetty.version}</version> 
      <type>pom</type> 
      <exclusions> 
       <exclusion> 
        <artifactId>org.eclipse.jdt.core</artifactId> 
        <groupId>org.eclipse.jetty.orbit</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency> 

     <!-- Jersey --> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-server</artifactId> 
      <version>${jersey.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-json</artifactId> 
      <version>${jersey.version}</version> 
     </dependency> 

     <!-- jUnit --> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>${junit.version}</version> 
      <scope>test</scope> 
     </dependency> 

     <!-- Apache Commons --> 
     <dependency> 
      <groupId>commons-io</groupId> 
      <artifactId>commons-io</artifactId> 
      <version>${apache.commons.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.httpcomponents</groupId> 
      <artifactId>httpclient</artifactId> 
      <version>${apache.http.version}</version> 
      <scope>test</scope> 
     </dependency> 

     <!-- PostgreSQL --> 
     <dependency> 
      <groupId>postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
      <version>${postgresql.jdbc.version}</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.mortbay.jetty</groupId> 
       <artifactId>jetty-maven-plugin</artifactId> 
       <version>8.1.14.v20131031</version> 
       <configuration> 
        <scanIntervalSeconds>10</scanIntervalSeconds> 
        <stopKey>foo</stopKey> 
        <stopPort>8080</stopPort> 
        <stopWait>10</stopWait> 

        <connectors> 
         <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> 
          <port>28080</port> 
          <maxIdleTime>60000</maxIdleTime> 
         </connector> 
        </connectors> 
       </configuration> 
       <executions> 
        <execution> 
         <id>start-jetty</id> 
         <phase>test-compile</phase> 
         <goals> 
          <goal>run</goal> 
         </goals> 
         <configuration> 
          <scanIntervalSeconds>0</scanIntervalSeconds> 
          <daemon>true</daemon> 
         </configuration> 
        </execution> 
        <execution> 
         <id>stop-jetty</id> 
         <phase>test</phase> 
         <goals> 
          <goal>stop</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 

      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>sql-maven-plugin</artifactId> 
       <version>${sql.maven.plugin.version}</version> 

       <dependencies> 
        <!-- Specify the dependent jdbc driver here --> 
        <dependency> 
         <groupId>postgresql</groupId> 
         <artifactId>postgresql</artifactId> 
         <version>${postgresql.jdbc.version}</version> 
        </dependency> 
       </dependencies> 

       <!-- Common configuration shared by all executions --> 
       <configuration> 
        <driver>org.postgresql.Driver</driver> 
        <url>jdbc:postgresql://localhost:5432:spozz_db</url> 
        <username>postgres</username> 
        <password>root</password> 
        <!-- You can comment out username/password configurations and have 
         maven to look them up in your settings.xml using ${settingsKey} --> 
        <settingsKey>sensibleKey</settingsKey> 
        <!-- All executions are ignored if -Dmaven.test.skip=true --> 
        <skip>${maven.test.skip}</skip> 
       </configuration> 

       <executions> 
        <execution> 
         <id>drop-schema-before-test-if-any</id> 
         <phase>process-test-resources</phase> 
         <goals> 
          <goal>execute</goal> 
         </goals> 
         <configuration> 
          <!-- Need another database to drop the targeted one --> 
          <url>jdbc:postgresql://localhost:5432:postgres</url> 
          <autocommit>true</autocommit> 
          <sqlCommand>DROP SCHEMA spozz CASCADE</sqlCommand> 
          <!-- Ignore error when database is not available --> 
          <onError>continue</onError> 
         </configuration> 
        </execution> 

        <execution> 
         <id>create-schema</id> 
         <phase>process-test-resources</phase> 
         <goals> 
          <goal>execute</goal> 
         </goals> 
         <configuration> 
          <autocommit>true</autocommit> 
          <srcFiles> 
           <srcFile>src/main/sql/spozz-schema.sql</srcFile> 
          </srcFiles> 
         </configuration> 
        </execution> 

       </executions> 
      </plugin> 

      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>dbunit-maven-plugin</artifactId> 
       <version>1.0-beta-3</version> 

       <dependencies> 
        <!-- Specify the dependent jdbc driver here --> 
        <dependency> 
         <groupId>postgresql</groupId> 
         <artifactId>postgresql</artifactId> 
         <version>${postgresql.jdbc.version}</version> 
        </dependency> 
       </dependencies> 

       <!-- Common configuration shared by all executions --> 
       <configuration> 
        <driver>org.postgresql.Driver</driver> 
        <url>jdbc:postgresql://localhost:5432:spozz_db</url> 
        <username>postgres</username> 
        <password>root</password> 
        <!-- You can comment out username/password configurations and have maven 
         to look them up in your settings.xml using ${settingsKey} --> 
        <settingsKey>sensibleKey</settingsKey> 
        <!-- All executions are ignored if -Dmaven.test.skip=true --> 
        <skip>${maven.test.skip}</skip> 
       </configuration> 

       <executions> 
        <execution> 
         <phase>test-compile</phase> 
         <goals> 
          <goal>operation</goal> 
         </goals> 
         <!-- Specific configurations --> 
         <configuration> 
          <type>CLEAN_INSERT</type> 
          <src>src/test/resources/spozz_db_testdata.xml</src> 
         </configuration> 
        </execution> 
       </executions> 

      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.4</version> 
       <executions> 
        <execution> 
         <id>copy-dependencies</id> 
         <phase>package</phase> 
         <goals><goal>copy-dependencies</goal></goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 

     <pluginManagement> 
      <plugins> 

       <!-- M2Eclipse Compatibility --> 
       <plugin> 
        <groupId>org.eclipse.m2e</groupId> 
        <artifactId>lifecycle-mapping</artifactId> 
        <version>1.0.0</version> 
        <configuration> 
         <lifecycleMappingMetadata> 
          <pluginExecutions> 
           <pluginExecution> 
            <pluginExecutionFilter> 
             <groupId>org.apache.maven.plugins</groupId> 
             <artifactId>maven-dependency-plugin</artifactId> 
             <versionRange>[2.4,)</versionRange> 
             <goals> 
              <goal>copy-dependencies</goal> 
             </goals> 
            </pluginExecutionFilter> 
            <action> 
             <execute /> 
            </action> 
           </pluginExecution> 
          </pluginExecutions> 
         </lifecycleMappingMetadata> 
        </configuration> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
    </build> 
</project> 

回答

0

如果這些都定期測試,這是在test階段執行,那麼事實你開始在pre-integration-test碼頭是你的問題。你應該在test-compile階段運行它。

+0

已更新my * pom.xml *文件,以便jetty從'test-compile'開始並停在'test'階段。但是,現在我得到了兩個新的錯誤。 '失敗:已經使用的地址'和'java.lang.NoClassDefFoundError:org/apache/jasper/runtime/JspApplicationContextImpl',但'ps -ef | grep 5000 | xargs kill'沒有發現端口5000上運行的服務被取消。並且似乎還有另一個連接錯誤:[警告]失敗[email protected]:8080 java.net.BindException:已在使用中的地址# – Phidelux

+0

如果我記得正確,在調試模式下運行Maven會在端口5000上啓動Maven。嘗試在8080上啓動Jetty,或者其他的東西。 – carlspring

+0

謝謝,切換到其他港口和改變階段幫助。但是,我仍然有問題,包括JSP。因此,我更新了我的'pom.xml'(附加在我的文章中)以使用更新版本的jetty。現在我收到新的錯誤。所以我去了GitHub並瀏覽其他項目,直到找到[this one](https://github.com/jetty-project/embedded-jetty-jsp)。現在我很困惑,因爲在新版本中似乎沒有必要在項目的'webapp/WEB-INF'文件夾中提供'web.xml'。所以我的問題是如果這是實現使用JSP的基於JAX-RS的web服務的新方法。 – Phidelux