简体   繁体   中英

how to define property in maven pom.xml and pass arguments from command line?

I need to pass two arguments, say shell and version from command line.

The issue is I am not able to understand how to define these two variables in pom.xml , and then fetch the parameters in my Java code at runtime.

Also when I run it as a Maven project , how do I set the parameter values?

Any suggestions would be of great help. Thanks!!

Option 1 - "storing" build arguments into compiled project to be retrieved when project is run

if you want to provide shell and version as arguments to the maven build at compile time (when you build your code into, say a *.jar file) and then, at some later point when your compiled code is run you want to retrieve those parameters back.

this is achievable by using the maven resources plugin's filtering capability :

suppose your project structure was

pom.xml
src/
   main/
      java/
         Main.java
      resources/
         strings.properties

you could put property placeholders into strings.properties:

shell=${my.shell}
version=${my.version}

and then in the pom.xml configure the maven resources plugin (that copies src/main/resources/* into the target directory where the jar plugin will later pick it up from) to do a find-and-replace on the file (called filtering):

<project>
  ...
  <name>bob</name>
  ...
  <build>
    ...
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>

you could supply these parameters as system properties to maven (using -D[name]=[value] on the command line), like so:

mvn clean install -Dmy.shell=X -Dmy.version=Y

then, if you were to look at the processed resource file (should be in /target) you would see the resources plugin has filtered the file into:

shell=X
version=Y

reading this back at runtime would be something like:

public class Main {
   public static void main (String[] args) {
      InputStream is = Main.class.getClassLoader().getResourceAsStream("strings.properties");
      Properties props = new Properties();
      props.load(is);
      System.out.println("shell is " + props.get("shell"));
   }
}

this is assumming your code is run "from inside" the jar file produced. various packaging/deployment form factors might requires slightly different ways of getting the input stream to read from.

Option 2 - accessing arguments provided to the maven build from code that runs as part of the build (say unit tests)

in this scenario youre not concerned with storing these params embedded into your compiled project anywhere, you just want to access them at build time (say from your unit test code).

any argument provided to java in the form (note no spaces allowed):

java <whatever> -Dprop.name=value

is available at runtime like this:

String propValue = System.getProperty("prop.name");

a slight complication here, specific to maven and unit tests, is that the maven surefire plugin (which is the plugin that runs unit tests) and also the maven failsafe plugin (which is the closely-related plugin that runs system tests) fork off a new JVM to run your unit tests in . however, according to their documentation:

System property variables from the main maven process are passed to the forked process as well

so the above solution should work. if it doesnt you can either configure the plugin not to fork off the unit tests:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <forkCount>0</forkCount>
    </configuration>
</plugin>

or alternatively pass thise parameters to the forked process yourself:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <systemPropertyVariables>
            <my.schema>${my.schema}</my.schema>
        </systemPropertyVariables>
    </configuration>
</plugin>

and then they should be accessible in test code via System.getProperty()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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