David Baak David Baak - 7 months ago 24
Java Question

Maven run test, then compile then other test

My goal is to enable Maven to execute in one build:


  1. Run a test in which a random username is written to a file in the resources

  2. Then compile and run tests (using the compiled resource)



What works

With
test -Dtestgroups="purchase,checkorders"
running the other tests (2).

What I miss

Running the username creation, then compiling and testing.

My config:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<configfailurepolicy>continue</configfailurepolicy>
<systemPropertyVariables>
<url>${url}</url>
<browser>${browser}</browser>
<language>${language}</language>
</systemPropertyVariables>
<includes>
<include>**/*.java</include>
</includes>
<groups>${testGroups}</groups>
<excludedGroups>${excludedGroups}</excludedGroups>
<parallel>classes</parallel>
<threadCount>${threadCount}</threadCount>
<forkMode>once</forkMode>
<workingDirectory>target/</workingDirectory>
</configuration>
</plugin>


I've read some posts about executions but no success so far.

UPDATE:

I managed to make a plugin. See my answer below.

Answer

I managed to make a plugin and also published it on my github account. My Mojo's pom:

<groupId>credentials.plugin</groupId>
<artifactId>credentials-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>Credentials Maven Plugin</name>

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>
    <!-- dependencies to annotations -->
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.4</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

The Mojo.

@Mojo(name = "credentials")
public class CredentialsMojo extends AbstractMojo {

  @Parameter(property = "credentials.propertiesDirectory", defaultValue = "empty")
  private String propertiesDirectory;

  @Parameter(property = "credentials.propertiesFileName", defaultValue = "credentials")
  private String propertiesFileName;

  @Parameter(property = "credentials.nameLength", defaultValue = "6")
  private int nameLength;

  @Parameter(property = "credentials.password", defaultValue = "Welkom01@")
  private String password;

  public void execute() throws MojoExecutionException {
    getLog().info("Creating "+ propertiesFileName +".properties");
    parseDirectoryName();

    try (OutputStream output = new FileOutputStream("credentials.properties")){
      tryCreateCredentialProperties(output);
    } catch (IOException io) {
      getLog().error(io);
    }
  }

  private void parseDirectoryName() {
    if (propertiesDirectory.equals("empty")) propertiesDirectory = "";
    addSlashToDirectory();
  }

  private OutputStream tryCreateCredentialProperties(OutputStream output) throws FileNotFoundException, IOException {
    final String username = RandomStringUtils.randomAlphanumeric(nameLength);
    Properties prop = new Properties();
    prop.setProperty("username", username);
    prop.setProperty("password", password);
    prop.store(output, null);
    getLog().info("Username = " + username);
    getLog().info("Password = " + password);
    return output;
  }
}

And in the project using the plugin:

<properties>
  <nameLength>16</nameLength>
  <password>qwerty@01</password>
  <propertiesDirectory>src/test/resources</propertiesDirectory>
  <propertiesFileName>foobar</propertiesFileName>
</properties>
...
<plugin>
  <groupId>credentials.plugin</groupId>
  <artifactId>credentials-maven-plugin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <configuration>
    <nameLength>${nameLength}</nameLength>
    <password>${password}</password>
    <propertiesDirectory>${propertiesDirectory}</propertiesDirectory>
    <propertiesFileName>${propertiesFileName}</propertiesFileName>
  </configuration>
</plugin>