2017-02-20 67 views
0

我的application.properties中有以下兩個REST URL。如何根據maven配置文件命中特定的REST URL?

我想獲取一個但不是基於動態參數,但不知道如何。我嘗試使用maven配置文件,但不知道如何閱讀Java代碼中的maven配置文件並獲取基於此的url。

請指導。

application.properties

rest.uri=http://localhost:8080/hello 
mock.rest.uri=http://localhost:9999/hello 

RestClient.java

public class HelloWorldClient { 

    public static void main(String[] args) { 
     try { 
      Client client = Client.create(); 

      //getRestUrl() METHOD CALL NEEDS TO BE DYNAMIC 
      //EITHER MOCK URL OR ACTUAL REST URL SHOULD BE FETCHED HERE 
      // NOT SURE HOW ??????? 
      WebResource webResource = client.resource(getRestUrl()); 

      ClientResponse response = webResource.accept("text/plain").get(ClientResponse.class); 
      if (response.getStatus() != 200) { 
       throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); 
      } 
      String output = response.getEntity(String.class); 
      System.out.println("\nOutput from Server.... " + output); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private static String getRestUrl() throws IOException { 
     Properties prop = GenericUtils.loadProperties("application.properties"); 
     String restUri = prop.getProperty("rest.uri"); 
     String mockRestUri = prop.getProperty("mock.rest.uri"); 
     System.out.println("restUri = " + restUri); 
     System.out.println("mockRestUri = " + mockRestUri); 
     return mockRestUri; 
    } 

} 

的pom.xml

<profiles> 
    <profile> 
     <id>rest-server</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
    </profile> 
    <profile> 
     <id>mock-rest-server</id> 
    </profile> 
</profiles> 
+0

你可以有你'application.properties'文件的多個版本編譯的類。根據Maven配置文件,在您的應用程序中打包正確的配置文件。 –

+0

你能指點我一些文檔或例子嗎? – user2325154

+0

爲什麼你甚至需要這樣做?將配置文件放在包含此URL的JAR之外,然後代碼將簡單地讀取該文件中的任何內容。然後您可以隨時編輯該文件,並重新啓動您的應用程序,不需要涉及任何奇怪的定製打包/配置文件。 – Tunaki

回答

1

基於上述Jose和Fetta提供的解決方案,我修改了程序,並將這兩個解決方案聚合到這篇文章中並在此處發佈。

application.properties

rest.uri=${rest.uri} 

的pom.xml

<build> 
    <filters> 
     <filter>src/main/resources/application.properties</filter> 
    </filters> 
    <resources> 
     <resource> 
      <directory>src/main/resources</directory> 
      <filtering>true</filtering> 
     </resource> 
    </resources> 
</build> 

<profiles> 
    <profile> 
     <id>rest-server</id> 
     <properties> 
      <rest.uri>http://localhost:8080/hello</rest.uri> 
     </properties> 
    </profile> 
    <profile> 
     <id>mock-rest-server</id> 
     <properties> 
      <rest.uri>http://localhost:9999/hello</rest.uri> 
     </properties> 
    </profile> 
</profiles> 

HelloWorldClient。java的

public class HelloWorldClient { 

    public static void main(String[] args) { 
     try { 
      Client client = Client.create(); 
      WebResource webResource = client.resource(getRestUrl()); 
      ClientResponse response = webResource.accept("text/plain").get(ClientResponse.class); 
      if (response.getStatus() != 200) { 
       throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); 
      } 
      String output = response.getEntity(String.class); 
      System.out.println("\nOutput from Server.... " + output); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private static String getRestUrl() throws IOException { 
     Properties prop = GenericUtils.loadProperties("application.properties"); 
     String restUri = prop.getProperty("rest.uri"); 
     System.out.println("restUri = " + restUri); 
     return restUri; 
    } 

} 

使用配置文件

mvn clean install -Prest-server 
mvn clean install -Pmock-rest-server 

運行的主要方法

mvn exec:java -Dexec.mainClass="com.example.HelloWorldClient" 
2

您可以DEF在一個屬性中,根據執行的maven配置文件,它將被填充一個或另一個值。

例如:

<profiles> 
    <profile> 
     <id>rest-server</id> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
     <properties> 
      <rest.uri>ttp://localhost:8080/hello</rest.uri> 
     </properties> 
    </profile> 
    <profile> 
     <id>mock-rest-server</id> 
     <properties> 
      <rest.uri>http://localhost:9999/hello</rest.uri> 
     </properties> 
    </profile> 
</profiles> 

現在,application.properties文件:

rest.uri=${rest.uri} 

行家的過濾插件將根據所執行的簡檔執行的值的替換。

從Java代碼,你可以隨時讀取相同的屬性,因爲它的值將模擬或實取決於已執行

1

您可以使用Maven Resources插件過濾資源Maven的配置文件。它的用法說明如下:

https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

然後你就可以在你的配置文件定義特定的配置文件的屬性。之後,在構建過程中,.properties文件將在構建過程中被過濾,您的應用程序可以使用它。 .properties值將根據構建期間激活哪個配置文件而有所不同。

這一切都在鏈接中描述。