简体   繁体   English

如何使用Maven为不同的配置文件执行任务

[英]how to perform task for different profiles using maven

I am new to Maven. 我是Maven的新手。

I want to perform database connection for different users so my problem is that where I should provide this JDBC connection and how to provide this connection for different users? 我想为不同的用户执行数据库连接,所以我的问题是我应该在哪里提供此JDBC连接,以及如何为不同的用户提供此连接?

I know how to provide profiles for different users but where should I perform database connection and how it get invoked? 我知道如何为不同的用户提供配置文件,但是我应该在哪里执行数据库连接以及如何调用它?

Best approach would make your database connection properties (like username/password, url, etc) external. 最好的方法是使数据库连接属性(如用户名/密码,URL等)处于外部。 Within a profile you could than define per user the values for the properties and use the maven resource filtering to set them. 在概要文件中,您可以为每个用户定义属性的值,然后使用Maven资源过滤进行设置。

Within your maven project you would for example have a config directory (in src/config/settings.prp ) which for example contains the following entries: 在您的maven项目中,例如,将有一个config目录(在src/config/settings.prp ),其中包含以下条目:

userName = ${userName}
password = ${password}
db-driver = ${dbDriver}
db-url = ${dbUrl}

Within the pom you would have 在pom中,您将拥有

<project ...>
    <build>
        <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-resources-plugin</artifactId>
               <version>2.7</version>
               <executions>
                   <execution>
                       <id>filter-db-settings</id>
                       <goals>
                           <goal>copy-resources</goal>
                       </goals>
                       <configuration>
                           <outputDirectory>${project.build.directory}/config</outputDirectory>
                           <resources>
                               <resource>
                                   <directory>${project.basedir}/src/config</directory>
                               </resource>
                               <filtering>true</filtering>
                           </resources>
                       </configuration> 
                   </execution>
               </executions>
           </plugin>
        <plugins>
    </build>

    <profiles>
        <profile>
            <id>user-A</id>
            <properties>
                <userName>userA</userName>
                <password>secret</password>
                <dbDriver>com.driver.db</dbDriver>
                <dbUrl>jdbc://db-url</dbUrl>
            </properties>
        </profile>
    <profiles>
</project>

The plugin will filter the files in src/config and replace the maven placeholders by the values as specified within your profile. 该插件将过滤src/config的文件,并用src/config文件中指定的值替换maven占位符。 Since the profile contains a password you could move it to your settings.xml so that it is not checked in with the project itself possibly exposing the password to unwanted parties. 由于配置文件包含密码,您可以将其移至settings.xml ,这样就不会与项目本身一起签入,这可能会使密码暴露给不需要的各方。

WARNING: Haven't verified the plugin above so might contain small mistakes. 警告:尚未验证上述插件,因此可能包含一些小错误。

Best practice it to put the property file NOT within the generated artifact. 最佳做法是将属性文件放在生成的工件中。 By doing this you get the freedom to use the same artifact fro different users and the only thing you need to change are the properties in the external property file, (which will be given to user beside the artifact). 这样,您就可以自由地在不同用户之间使用同一工件,而唯一需要更改的就是外部属性文件中的属性(该属性将在工件旁边提供给用户)。

The following article explains how you can externalize the properties with spring Externalized Configuration . 以下文章说明了如何使用spring 外部配置来外部化属性。

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

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