问题
I wrote some unit test code that prints out the value of a System.property named "JENKINS_BUILD". This property has a value when I run these command locally:
mvn --batch-mode test -DJENKINS_BUILD=true
mvn test -DargLine="-DJENKINS_BUILD=true"
etc.
But no matter what I do, I cannot get this property into my tests when they are run on Jenkins. This is the command line option I'm using there:
mvn -DargLine="-DJENKINS_BUILD=true" --batch-mode -Dresume=false release:prepare release:perform
Slightly different, but I don't see why it would matter. Why is System.getProperty("JENKINS_BUILD")
null on Jenkins but "true" in my local environment? Does this have something to do with using release:prepare release:perform
?
I think this is basically the same question, but I'm not sure. The difference is he's trying to pass arguments into the release plugin, I'm trying to pass them into the surefire plugin. Also, I want to define the value in the command line without having to change my pom.xml. Is there a way to do that?
FWIW, the solution that I linked to does work.
回答1:
Note that if you do not fork your JVM, the properties you set on the maven command line are found; but, if you do fork your JVM the properties need to be copied on the forked JVM's argment list.
I am guessing that you are forking some aspect of your build, but not passing in a new argline, or are passing in a new argline that doesn't copy the necessary value from the launched (set) property.
This might mean that you could need to set a default (empty) property to make your argline configuration correct in both cases where the argument is set and where the argument is not set. That's what the second "example" in your link (Sander Verhagen's example) is detailing, even if it isn't explicit as to why.
As far as why this is often encountered in the maven-release-plugin, keep in mind that during the maven-release-plugin you have one "parent" maven process driving two complete nested maven builds. Failure to pass arguments to the children builds will mean the children run without the required arguments (as they are only argument to the parent activity, not the two maven runs to build the release version and the next snapshot version.
来源:https://stackoverflow.com/questions/29088735/why-cant-my-tests-see-my-system-property-when-i-release-on-jenkins