2017-02-20 1 views
0

Ich habe die folgenden zwei REST-URLs in meiner application.properties.Wie wird eine bestimmte REST-URL basierend auf dem maven-Profil erstellt?

Ich möchte eine, aber nicht beide basierend auf dynamische Parameter abrufen, aber nicht sicher, wie. Ich habe versucht, Maven-Profil zu verwenden, aber nicht sicher, wie man das Maven-Profil in Java-Code liest und die URLs darauf basiert.

Bitte führen.

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

die wichtigste Methode

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

Run können mehrere Versionen Ihrer 'application.properties' Datei haben könnte. Packen Sie je nach Maven-Profil den richtigen in Ihre Anwendung ein. –

+0

Können Sie mich auf eine Dokumentation oder ein Beispiel hinweisen? – user2325154

+0

Warum müssen Sie das überhaupt tun? Platzieren Sie eine Konfigurationsdatei außerhalb der JAR, die diese URL enthält, und der Code würde dann einfach das lesen, was sich in dieser Datei befindet. Sie können diese Datei dann jederzeit bearbeiten und Ihre App neu starten, ohne dass dabei merkwürdige benutzerdefinierte Verpackungen/Profile erforderlich sind. – Tunaki

Antwort

1

Basierend auf der Lösung von Jose und Fetta oben habe ich das Programm geändert und hiermit die beiden Lösungen in diesen Beitrag zusammengefasst und hier gepostet.

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

} 

Kompilieren Sie die Klasse Profile

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

können Sie def in einer einzigen Eigenschaft und abhängig von dem Mavenprofil, das ausgeführt wird, wird es mit dem einen oder anderen Wert gefüllt.

Zum Beispiel:

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

nun application.properties Datei:

rest.uri=${rest.uri} 

Die Filterung Plugin von Maven wird die Substitution des Wertes durchführen entsprechend dem Profil, das ausgeführt wird.

aus dem Java-Code, den Sie können die gleiche Eigenschaft immer lesen, weil es den Wert Mock hat oder real in Abhängigkeit von dem Maven-Profil, die

1

Sie Maven Plugin Ressourcen zur Filterung Ressourcen verwenden können ausgeführt wurde. Es ist Nutzung ist hier beschrieben:

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

Dann können Sie profilspezifische Eigenschaften in Ihre Profile definieren. Danach wird Ihre .properties-Datei während des Builds gefiltert und von Ihrer Anwendung verwendet. Der Wert für .properties ist abhängig davon, welches Profil während des Builds aktiviert wurde.

Es ist alles in dem Link beschrieben.

Verwandte Themen