How to inject properties or environment variables into a Maven plugin test file?

I've created a Maven plugin using AbstractMojo and I'm trying to test it.

I am using maven-plugin-testing-harness for testing and I am having trouble entering values ​​for my plugin parameters.

I have the following pom.xml ( src/test/resources/pom.xml

) file for testing:

<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>com.vodori.pepper.docker.vm.unit</groupId>
    <artifactId>test-pom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Test VMStarter</name>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.vodori.common</groupId>
                <artifactId>pepper-docker-vm</artifactId>
                <version>1.0.1-SNAPSHOT</version>
                <configuration>
                    <dockerSnapshotSite>pepper-demo-site</dockerSnapshotSite>
                    <dockerSnapshotVersion>3.6.11</dockerSnapshotVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

      

My Mojo looks like this (top):

public abstract class VMPlugin extends AbstractMojo {

    /**
     * Docker location
     */
    @Parameter(required = true, property = "docker.path", defaultValue = "${env.DOCKER_LOCATION}")
    String dockerPath;

    public void setDockerPath(String dockerPath) {
        this.dockerPath = dockerPath;
    }

    /**
     * Docker VM Site name
     */
    @Parameter(required = true, property = "docker.snapshot.site")
    String dockerSnapshotSite;

    /**
     * Version of Docker snapshot
     */
     @Parameter (required = true, property="docker.snapshot.version")
    String dockerSnapshotVersion;

      

I am using @MojoRule method for testing and my setup method looks like this:

@Before
public void setUp() throws Exception {
    vmStarter = (VMStarter) rule.lookupMojo( "start-docker-vm", "src/test/resources/pom.xml" );
    assertNotNull(vmStarter);
}

      

I use a setter for some of my test tables (the ones testing bad docker locations), but for my good testing of the path, I want to rely on an environment variable DOCKER_LOCATION

to populate. However, for some reason dockerPath

, it only displays as null

. It seems to be defaultValue

ignored.

I tried to dump System.getEnv()

to STDERR and I see that it is DOCKER_LOCATION

indeed installed.

What am I missing here? Why is @Parameter

n't mine filling in correctly?

+3


source to share


1 answer


Where did you get the syntax defaultValue = "${env.DOCKER_LOCATION}"

from?

env.*

is a property and "you can use Maven properties in the pom.xml file or on any resource that is handled by the filtering functionality of the Maven Resource plugins."

default-value

expression required.

Java Plugin Development Guide, Introduction mentions: "(more can be found in the document Parameter Expressions)". But I haven't found such a document yet. thanks @khmarbaise: org.apache.maven.plugin.PluginParameterExpressionEvaluator .



help:expressions

doesn't show ${env}

with my Maven 3.2.1.

Although my experience is that at least some, if not all of them are document-related, not updated, or contain the latest improvements.

Possible explanation from a logical point of the program: if a default value is to be set outside the scope of the program, it cannot be considered a default value. In the Maven sense, a convention over configuration .

EDIT: Added link to API Expressions documentation.

+1


source







All Articles