如何将变量作为数据库密码设置到pom.xml中

时间:2017-01-30 15:08:39

标签: java maven jenkins environment-variables pom.xml

我需要将Jenkins变量配置为我的密码数据库到我的pom的配置文件中,但是如果我设置${env.passwordb}(如下所示),项目不会编译并出现一些错误,但如果我将代码推送到Jenkins,它接受变量并完美地运行项目。我需要改变什么?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
          http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company.vrs</groupId>
    <artifactId>company-vrs</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>${env.urlTomcat}</url>
                    <path>${env.appPath}</path>
                    <username>${env.username}</username>
                    <password>${env.password}</password>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <packaging>war</packaging>
    <name>company-vrs</name>
    <url>http://maven.apache.org</url>
    <profiles>
        <profile>
            <id>dev</id>
            <build>
                <finalName>visitors</finalName>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                        <excludes>
                            <exclude>**/*.jks</exclude>
                        </excludes>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>false</filtering>
                        <includes>
                            <include>**/*.jks</include>
                        </includes>
                    </resource>
                </resources>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.3</version>
                        <configuration>
                            <mainClass>com.company.vrs.config.AppConfig</mainClass>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.4</version>
                        <configuration>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.eclipse.jetty</groupId>
                        <artifactId>jetty-maven-plugin</artifactId>
                        <version>9.2.0.RC0</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>asta que
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <port>9090</port>
                            <path>/</path>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
            <properties>
                <db.url>jdbc:postgresql://hsvvrsdev.company.com:5432/vrs_dev</db.url>
                <db.username>vrsuser</db.username>
                <db.password>${env.passworddb}</db.password>
                <isdev>true</isdev>
                <realm.metadata.file>/metadata/secureAuth_dev.xml</realm.metadata.file>
                <realm.metadata.url>https://ssodev.company.com/SecureAuth68/metadata.xml</realm.metadata.url>
                <realm.url.restart>https://ssodev.company.com/SecureAuth68/restart.aspx</realm.url.restart>
            </properties>
        </profile>
        <profile>
            <id>sqa</id>
            <build>
                <finalName>visitors_sqa</finalName>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                        <excludes>
                            <exclude>**/*.jks</exclude>
                        </excludes>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>false</filtering>
                        <includes>
                            <include>**/*.jks</include>
                        </includes>
                    </resource>
                </resources>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.3</version>
                        <configuration>
                            <mainClass>com.company.vrs.config.AppConfig</mainClass>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.4</version>
                        <configuration>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.eclipse.jetty</groupId>
                        <artifactId>jetty-maven-plugin</artifactId>
                        <version>9.2.0.v20140526</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <port>9090</port>
                            <path>/</path>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
            <properties>
                <db.url>jdbc:postgresql://hsvvrsdev.company.com:5432/vrs_sqa</db.url>
                <db.username>vrsuser</db.username>
                <db.password>vrs#2015!</db.password>
                <istest>true</istest>
                <realm.metadata.file>/metadata/secureAuth_dev.xml</realm.metadata.file>
                <realm.metadata.url>https://ssodev.company.com/SecureAuth68/metadata.xml</realm.metadata.url>
                <realm.url.restart>https://ssodev.company.com/SecureAuth68/restart.aspx</realm.url.restart>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <build>
                <finalName>visitors</finalName>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                        <excludes>
                            <exclude>**/*.jks</exclude>
                        </excludes>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>false</filtering>
                        <includes>
                            <include>**/*.jks</include>
                        </includes>
                    </resource>
                </resources>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.3</version>
                        <configuration>
                            <mainClass>com.company.vrs.config.AppConfig</mainClass>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.4</version>
                        <configuration>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.eclipse.jetty</groupId>
                        <artifactId>jetty-maven-plugin</artifactId>
                        <version>9.2.0.v20140526</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <port>9090</port>
                            <path>/</path>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
            <properties>
                <db.url>jdbc:postgresql://hsvvrsdb.company.com:5432/vrs</db.url>
                <db.username>vrsuser</db.username>
                <db.password>v14r3s20150302</db.password>
                <ispro>true</ispro>
                <realm.metadata.file>/metadata/secureAuth_prod.xml</realm.metadata.file>
                <realm.metadata.url>https://sso.company.com/visitors/metadata.xml</realm.metadata.url>
                <realm.url.restart>https://sso.company.com/visitors/restart.aspx</realm.url.restart>
            </properties>
        </profile>
    </profiles>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-version>4.1.2.RELEASE</spring-version>
        <spring-data-jpa-version>1.6.1.RELEASE</spring-data-jpa-version>
        <spring-data-commons-version>1.8.1.RELEASE</spring-data-commons-version>
        <hibernate-version>4.3.5.Final</hibernate-version>
        <org-slf4j-version>1.7.7</org-slf4j-version>
        <spring-ldap-version>2.0.1.RELEASE</spring-ldap-version>
        <spring-security-version>3.2.3.RELEASE</spring-security-version>
        <bonecp-version>0.8.0.RELEASE</bonecp-version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
           ..... many dependencies ....        
    </dependencies>
</project>

错误:

  

引起:java.lang.IllegalArgumentException:无法解决   占位符'env.passworddb'在字符串值“$ {env.passworddb}”

1 个答案:

答案 0 :(得分:3)

经过一番调查后发现$ {env.passwddb}仅存在于远程jenkins环境中。为了克服这个问题并仍然使用相同的配置文件配置,可能的解决方案是:

1)硬编码

在本地环境中,密码应该在属性文件中进行硬编码。一个遥控器,占位符仍然存在。

2)密码通过maven命令行传递:

mvn clean package resources:testResources -Denv.passwddb=realpass