简体   繁体   English

使用Jenkins,Maven和JVM参数进行参数化webapp

[英]Parametrize webapp using Jenkins, Maven and JVM parameters

Two objectives: 两个目标:

  1. I want to use parametrized jenkins builds to build a war file and set some properties during the build in the war file. 我想使用参数化的jenkins构建来构建war文件并在war文件中的构建过程中设置一些属性。

  2. I also want to be able to refine those properties on the server, where the war file is deployed. 我还希望能够在部署war文件的服务器上优化这些属性。

Number 1 is to set properties that fit to the target environment, Number 2 is to be able to quickly change them without having to rebuild the whole application again. 1号是要设置适合目标环境的属性,2号是要能够快速更改它们,而不必再次重新构建整个应用程序。

Choosing a maven profile is not flexible enough in this case. 在这种情况下,选择Maven配置文件不够灵活。

An example would be a port number, that is different for every build, but can spontaneously be changed by the system administrator of the system where the file is deployed. 一个示例是端口号,该端口号对于每个构建都是不同的,但是可以由部署文件的系统的系统管理员自发更改。

My idea was to use the maven-resource-filtering plugin to add the build parameters on build into property files. 我的想法是使用maven-resource-filtering插件将构建时的构建参数添加到属性文件中。 Then on startup of the webapp on the glassfish/tomcat to also look at the set JVM variables. 然后在glassfish / tomcat上启动webapp时,还要查看设置的JVM变量。

Am I thinking in the right direction? 我在想正确的方向吗?

After some research the solution for me looks as followed, for a given parameter "test": 经过研究后,对于给定的参数“ test”,对我来说解决方案如下:

To objective 1.: Use command line parameters when executing maven build, eg use: 目标1:执行maven构建时使用命令行参数,例如,使用:

install -Dtest=OnBuildValue

Then use the maven resources plugin, that replaces strings like test=${test} in property files, through the given parameter on build. 然后使用Maven资源插件,该插件通过构建时给定的参数替换属性文件中的字符串test=${test} Add: 加:

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

To objective 2.: To be able to change the parameter "test" in the container, without having to rebuild the war file, add a JVM parameter -DTEST=ContainerValue . 对于目标2 ::为了能够在容器中更改参数“ test”而不必重建war文件,请添加JVM参数-DTEST=ContainerValue

Now we need some logic: 现在我们需要一些逻辑:

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

The methods are static because I use them to initialize constants like this: 这些方法是静态的,因为我使用它们来初始化常量,如下所示:

public static final String TEST_STRING;

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

Those constants can then be read from everywhere inside your project. 然后可以从项目内部的任何位置读取这些常量。 How you call this logic, or if you can implement it differently is up to you. 如何称呼此逻辑,或者是否可以以不同的方式实现它取决于您自己。 I'm sure there are nicer ways, and I would be glad to hear about your implementation, but this works for me. 我敢肯定有更好的方法,很高兴听到您的实现,但这对我有用。

Note to 1.: If you use Eclipse you can define the maven parameters under Run->Run configurations-> Goals. 注:1 .:如果使用Eclipse,则可以在“运行”->“运行配置”->“目标”下定义maven参数。 BUT if you use the glassfish tools (m2e plugin) to deploy your project to your glassfish, it will not use the run configuration. 但是,如果您使用glassfish工具(m2e插件)将项目部署到glassfish,它将不会使用运行配置。 You then have to create the file .m2/settings.xml looking like this: 然后,您必须创建文件.m2 / settings.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM