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

前端 未结 1 1594
春和景丽
春和景丽 2021-01-14 23:48

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 va

1条回答
  •  清酒与你
    2021-01-15 00:26

    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):

    
      ...
      bob
      ...
      
        ...
        
          
            src/main/resources
            true
          
          ...
        
        ...
      
      ...
    
    

    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  -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:

    
        org.apache.maven.plugins
        maven-surefire-plugin
        2.19.1
        
            0
        
    
    

    or alternatively pass thise parameters to the forked process yourself:

    
        org.apache.maven.plugins
        maven-surefire-plugin
        2.19.1
        
            
                ${my.schema}
            
        
    
    

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

    0 讨论(0)
提交回复
热议问题