2014-09-01 19 views
11

Có thể trong maven để thiết lập thuộc tính hệ thống có thể đạt được từ bên trong một lớp java hay không.Đặt một biến hệ thống trong một cấu hình maven

Tôi đã thấy rằng điều này là có thể (here) trong plugin chắc chắn như sau;

String param = System.getProperty("my_parameter1"); 

<configuration> 
    <systemPropertyVariables> 
     <my_property1>${my_property1}</my_property1> 
    </systemPropertyVariables> 
</configuration> 

Tuy nhiên tôi muốn có được một xử lý đối với môi trường tôi đang làm việc trong, tôi đã đi qua sản hoặc dev như một đối số hồ sơ maven - là nó có thể bằng cách nào đó để có được một xử lý trong các mã trên này hoặc từ thiết lập một biến trong hồ sơ tôi gọi và sau đó gọi system.getProperty hoặc một số cách khác?

Cảm ơn

tập tin pom tôi

<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>core</groupId> 
    <artifactId>core</artifactId> 
    <version>1.0</version> 
    <packaging>jar</packaging> 
    <build> 
     <sourceDirectory>src</sourceDirectory> 
     <testSourceDirectory>test</testSourceDirectory> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-jar-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <warSourceDirectory>WebContent</warSourceDirectory> 
        <failOnMissingWebXml>false</failOnMissingWebXml> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.17</version> 
       <configuration> 
        <parallel>methods</parallel> 
        <threadCount>10</threadCount> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>sonar-maven-plugin</artifactId> 
       <version>2.1</version> 
      </plugin> 
     </plugins> 
     <resources> 
      <resource> 
       <directory>src</directory> 
       <excludes> 
        <exclude>**/*.java</exclude> 
       </excludes> 
      </resource> 
      <resource> 
       <directory>resources</directory> 
       <includes> 
        <include>**/*.png</include> 
       </includes> 
      </resource> 
     </resources> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>commons-dbcp</groupId> 
      <artifactId>commons-dbcp</artifactId> 
      <version>1.4</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-lang</groupId> 
      <artifactId>commons-lang</artifactId> 
      <version>2.3</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-codec</groupId> 
      <artifactId>commons-codec</artifactId> 
      <version>1.9</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-core</artifactId> 
      <version>2.3.0</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-annotations</artifactId> 
      <version>2.3.0</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
      <version>2.3.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.mariadb.jdbc</groupId> 
      <artifactId>mariadb-java-client</artifactId> 
      <version>1.1.7</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
     </dependency> 
     <dependency> 
      <groupId>org.mockito</groupId> 
      <artifactId>mockito-all</artifactId> 
      <version>1.9.5</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.facebook4j</groupId> 
      <artifactId>facebook4j-core</artifactId> 
      <version>[2.0,)</version> 
     </dependency> 
     <dependency> 
      <groupId>com.relayrides</groupId> 
      <artifactId>pushy</artifactId> 
      <version>0.3</version> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.16</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.mail</groupId> 
      <artifactId>javax.mail</artifactId> 
      <version>1.5.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.3.6.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.mail</groupId> 
      <artifactId>javax.mail-api</artifactId> 
      <version>1.5.2</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.activation</groupId> 
      <artifactId>activation</artifactId> 
      <version>1.1.1</version> 
     </dependency> 
     <dependency> 
      <groupId>com.threewks.thundr</groupId> 
      <artifactId>thundr-mailgun</artifactId> 
      <version>1.1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-io</artifactId> 
      <version>1.3.2</version> 
     </dependency> 
     <dependency> 
      <groupId>com.google.code.gson</groupId> 
      <artifactId>gson</artifactId> 
      <version>2.3</version> 
     </dependency> 
    </dependencies> 
    <profiles> 
     <profile> 
      <id>DEV</id> 
      <properties> 
       <swifte.url>jdbc:mariadb://ip:3306/swifte?autoReconnect=true</swifte.url> 
       <swifte.username>user</swifte.username> 
       <swifte.password>pass</swifte.password> 
      </properties> 
      <build> 
       <resources> 
        <resource> 
         <directory>resources</directory> 
         <includes> 
          <include>JavaPNSDev.p12</include> 
         </includes> 
        </resource> 
       </resources> 
      </build> 
     </profile> 
     <profile> 
      <id>PROD</id> 
      <properties> 
       <swifte.url>jdbc:mariadb://ip:3306/swifte?autoReconnect=true</swifte.url> 
       <swifte.username>username</swifte.username> 
       <swifte.password>pass</swifte.password> 
      </properties> 
      <build> 
       <resources> 
        <resource> 
         <directory>resources</directory> 
         <includes> 
          <include>JavaPNSProd.p12</include> 
         </includes> 
        </resource> 
       </resources> 
      </build> 
     </profile> 
    </profiles> 
</project> 

Trả lời

13

Bạn nên kiểm tra exec-maven-plugin

Với cấu hình như sau (chú ý <systemProperties>) ...

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.3.2</version> 
    <executions> 
     <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>java</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <mainClass>com.example.Main</mainClass> 
     <arguments> 
      <argument>argument1</argument> 
     </arguments> 
     <systemProperties> 
      <systemProperty> 
       <key>hello.world</key> 
       <value>Hello Stack Overflow!</value> 
      </systemProperty> 
     </systemProperties> 
    </configuration> 
</plugin> 

..và lớp sau ...

package com.example; 
public class Main { 
    public static void main(String[] args) { 
     String prop = System.getProperty("hello.world"); 
     System.out.println(prop); 
    } 
} 

..aand chạy một package (chú ý giai đoạn trong cấu hình - bạn có thể thay đổi nếu bạn muốn, có thể cài đặt) nó in ra giá trị Hello Stack Overflow! từ phím hello.world. Vì vậy, về cơ bản plugin thực thi chương trình của bạn khi bạn xây dựng.

Xem thêm exec:exec goal. Trong ví dụ này, tôi đã sử dụng mục tiêu exec:java, nhưng cả hai đều khác nhau về cách chúng hoạt động.


CẬP NHẬT

Hiện nay tôi đang thiết lập một số giá trị trong thuộc tính dựa trên hồ sơ cá nhân trong tập tin maven pom của tôi. Có thể thiết lập thuộc tính hệ thống này trong cấu hình không? bởi vì thực sự, tôi chỉ có một tập tin pom cho dev và prod và trong hồ sơ của nó tôi sẽ cần phải thiết lập nó.

Có, chỉ cần sử dụng ${property.name} trong phần tử <value> của phần tử thuộc tính hệ thống. Ví dụ

<profiles> 
    <profile> 
     <activation> 
      <activeByDefault>true</activeByDefault> 
     </activation> 
     <id>world</id> 
     <properties> 
      <hello.world>Hello World!</hello.world> 
     </properties> 
    </profile> 
    <profile> 
     <activation> 
      <activeByDefault>false</activeByDefault> 
     </activation> 
     <id>stack</id> 
     <properties> 
      <hello.world>Hello Stack Overflow</hello.world> 
     </properties> 
    </profile> 
</profiles> 

Và plugin <systemProperties>

<systemProperties> 
    <systemProperty> 
     <key>hello.world</key> 
     <value>${hello.world}</value> 
    </systemProperty> 
</systemProperties> 

Chỉ cần bằng cách thay đổi cấu hình, hoặc là stack hoặc world, thông điệp sẽ in Hello Stack Overflow hay Hello World, tương ứng.


CẬP NHẬT 2

Plugin khác là properties-maven-plugin.Không có gì được thực hiện trên nó trong một thời gian, nhưng từ một vài bài kiểm tra chức năng cần thiết là có.

Nó có một mục tiêu set-system-properties cùng với một số khác useful goals để giúp tính một cách dễ dàng quản lý

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>properties-maven-plugin</artifactId> 
    <version>1.0-alpha-2</version> 
    <executions> 
     <execution> 
      <!-- any phase before your app deploys --> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>set-system-properties</goal> 
      </goals> 
      <configuration> 
       <properties> 
        <property> 
         <name>hello.world.two</name> 
         <value>Hello World!</value> 
        </property> 
       </properties> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 
+0

vì vậy tôi muốn sử dụng plugin maven exec trái ngược với những gì tôi hiện đang sử dụng là maven-biên dịch-plugin? – Biscuit128

+0

Không họ có thể đi cùng nhau. Plugin exec chỉ chịu trách nhiệm khởi chạy chương trình. Plugin trình biên dịch không làm điều đó. –

+0

một câu hỏi cuối cùng nếu đó là ok? Hiện tại tôi đang thiết lập một số giá trị trong các thuộc tính dựa trên cấu hình trong tệp maven pom của tôi. Có thể thiết lập thuộc tính hệ thống này trong cấu hình không? bởi vì thực sự, tôi chỉ có một tập tin pom cho dev và prod và trong hồ sơ của nó tôi sẽ cần phải thiết lập nó. Tôi sẽ đăng bài của tôi bây giờ – Biscuit128

Các vấn đề liên quan