2017-02-20 24 views
0

私はapplication.propertiesに次の2つの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

を使用してクラスをコンパイルします。 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; 
    } 

} 

は、あなたがあなたの `application.properties`ファイルの複数のバージョンを持つことができプロファイル

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

を実行し、mainメソッド

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

1つのプロパティがあり、実行されるMavenプロファイルに応じて、1つの値で満たされます。例えば

<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} 

フィルタリングプラグインが実行されたプロファイルに応じた値の代入を実行します。それはあなたがフィルタリングリソースのためにMavenのリソースプラグインを使用することができます

1

を実行されたMavenのプロファイルに応じた値のモックやリアルを持つことになりますので、あなたが常に同じプロパティを読み取ることができるJavaコードから

。これの使い方はここで説明されています

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

は、次に、あなたのプロファイルでプロファイル固有のプロパティを定義することができます。その後、ビルド中に.propertiesファイルがビルド中にフィルタリングされ、アプリケーションで使用できるようになります。 .properties値は、ビルド中にアクティブ化されたプロファイルによって異なります。

これはすべてリンクに記載されています。

関連する問題