2016-07-15 64 views
0

兩個目標:webapp的參數多參數

  1. 我想使用參數化詹金斯建立在戰爭文件的生成過程中建立一個戰爭文件,並設置一些屬性。

  2. 我也希望能夠改進服務器上的這些屬性,其中部署了war文件。

1號是設置適合的目標環境特性,2號是能夠迅速改變,而不必再重建整個應用程序。

在這種情況下選擇maven配置文件不夠靈活。

一個例子是端口號,每個版本都不同,但可以由部署文件的系統的系統管理員自發更改。

我的想法是使用maven-resource-filtering插件將構建參數添加到屬性文件中。然後啓動glassfish/tomcat上的webapp以查看設置的JVM變量。

我在正確的方向思考?

+1

Spring已經提供了開箱即用的功能...那麼爲什麼要自己滾動? –

+0

我建議您在啓動時從應用程序中讀取部署之外的配置文件並進行配置。 –

+1

@ M.Deinum你會介意命名該功能,還是鏈接文檔?我不確定在哪裏可以找到它。 – hFonti

回答

0

我的解決方案看起來是跟隨經過一番研究, 給定參數「測試」:

目標1:使用命令行參數執行Maven構建時,如使用:

install -Dtest=OnBuildValue

然後使用maven資源插件,通過build上的給定參數替換屬性文件中的字符串test=${test}。地址:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-resources-plugin</artifactId> 
    <version>${maven.resources.version}</version> 
    <configuration> 
     <encoding>UTF-8</encoding> 
    </configuration> 
</plugin> 
... 
<build> 
... 
    <resources> 
     <resource> 
      <directory>src/main/resources</directory> 
      <includes> 
       <include>**/*.properties</include> 
      </includes> 
      <filtering>true</filtering> 
     </resource> 
     <resource> 
      <directory>src/main/resources</directory> 
      <excludes> 
       <exclude>**/parameters.properties</exclude> 
      </excludes> 
      <filtering>false</filtering> 
     </resource> 
    </resources> 
.... 
</build> 

目標2:爲了能夠改變在容器中的參數「測試」,而無需重新構建war文件,添加一個JVM參數-DTEST=ContainerValue

現在,我們需要一些邏輯:

private static String getBuildParameter(String paramName) 
     throws IOException { 
    Resource resource = new ClassPathResource(
      "/META-INF/spring/parameters.properties"); 
    Properties props = PropertiesLoaderUtils.loadProperties(resource); 
    return props.getProperty(paramName); 
} 

public static String getParameter(final String paramName, 
     final String defaultValue, final String logMessage) { 
    String value = System.getProperty(paramName); 
    if (value!=null) { 
     Logger.getLogger(ParameterManager.class.getName()).log(
       Level.WARNING, 
       "Parameter: " + paramName + ": " + value 
         + " found in JVM parameters."); 
     return value; 
    } 
    try { 
     value = getBuildParameter(paramName.toLowerCase()); 
    } catch (IOException e) { 
     // catch, log exception... 
    } 
    if (value!=null) { 
     Logger.getLogger(ParameterManager.class.getName()).log(
       Level.WARNING, 
       "Parameter: " + paramName + ": " + value 
         + " found in parameters set on build time."); 
     return value; 
    } 

    Logger.getLogger(ParameterManager.class.getName()).log(
      Level.WARNING, 
      "Parameter: " + paramName + ": " + defaultValue 
        + " as default parameter. " + logMessage); 
    return defaultValue; 
} 

的方法是靜態的,因爲我用它們來初始化常數是這樣的:

public static final String TEST_STRING; 

static { 
    TEST_STRING = getParameter("TEST", "default value", 
      "The default is set, please ensure, that this is intended!"); 
} 

這些常量就可以從任何地方你的項目中讀取。你如何稱呼這一邏輯,或者如果你能以不同的方式實施,則取決於你。我確信有更好的方法,我很樂意聽到你的實現,但這對我很有用。

Note to 1 .: 如果您使用Eclipse,您可以在Run-> Run configurations-> Goals下定義maven參數。 但是如果您使用glassfish工具(m2e插件)將您的項目部署到您的glassfish,它將不會使用運行配置。然後您必須創建文件.m2 /設置。xml看起來像這樣:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
         https://maven.apache.org/xsd/settings-1.0.0.xsd> 
    <localRepository/> 
    <interactiveMode/> 
    <usePluginRegistry/> 
    <offline/> 
    <pluginGroups/> 
    <servers/> 
    <mirrors/> 
    <proxies/> 
    <profiles> 
     <profile> 
      <id>m2e</id> 
      <activation> 
       <property> 
        <name>m2e.version</name> 
       </property> 
      </activation> 
      <properties> 
       <test>xmlparameter</test> 
      </properties> 
     </profile> 
    </profiles> 
    <activeProfiles/> 
</settings>