访问maven属性插件

时间:2015-11-12 14:12:17

标签: java maven properties-file

我在回答此问题Specify system property to Maven project时提到了maven属性插件。为了能够从文件设置我的数据库属性(并在测试环境中使用maven参数覆盖它)。但是,如果我尝试访问其中一个属性     例如System.getProperty("mysql.url")返回null。如何从设置属性目标访问属性集。

private void initializeDatabaseConfiguration() {
    url = System.getProperty("mysql.url");
    con = DriverManager.getConnection(url, username, password);

}


   <plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>properties-maven-plugin</artifactId>
       <version>1.0-alpha-2</version>
       <executions>
           <execution>
               <phase>initialize</phase>
               <goals>
                   <goal>set-system-properties</goal>
               </goals>
               <configuration>
                   <files>                          
                      <file>src/main/resources/config.properties</file>
                   </files>
                   <properties>
                       <property>
                           <name>mysql.url</name>
                           <value>${mysql.url}</value>
                       </property>
                   </properties>
               </configuration>
           </execution>
       </executions>
   </plugin>

1 个答案:

答案 0 :(得分:0)

尝试添加阶段:

private List<string> DemoDistinct()
{
    List<string> dateList = new List<string>();
    DataTable dt = new DataTable();

    using (OleDbConnection cn = new OleDbConnection { ConnectionString = ConnectionString(System.IO.Path.Combine(Application.StartupPath, "WS1.xlsx"), "Yes") })
    {
        cn.Open();

        using (OleDbCommand cmd = new OleDbCommand
        {
            CommandText = "SELECT DISTINCT [Dates] FROM [Sheet2$]",
            Connection = cn
        }
         )
        {
            OleDbDataReader dr = cmd.ExecuteReader();
            dt.Load(dr);
            dateList = dt
                .AsEnumerable()
                .Select(row => row.Field<DateTime>("Dates").ToShortDateString()).ToList();                      
        }
    }

    return dateList;
}

如果你使用阶段初始化,你可以在阶段之后使用这些主人&#39;初始化&#39;