2016-04-29 97 views
0

我的目标是使Maven的一个构建执行:Maven的运行测试,然后编译然后其他测试

  1. 运行在一个随机用户名被写入文件中的资源
  2. 然后测试编译并运行测试(使用编译的资源)

什么工作
随着test -Dtestgroups="purchase,checkorders"运行的其他(2)测试。

我想念的是
运行用户名创建,然后编译和测试。

我的配置:

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

我读过有关处决一些帖子,但没有成功为止。

UPDATE:
我设法让一个插件。请参阅下面的答案。

+1

我认为你需要做的,是写一个魔力来处理你的intiial测试(1)。将此插件添加到相关的pom中时,可以设置执行参数,以便在编译生命周期中比编译阶段更早运行,例如在验证阶段或生成阶段。请参阅此链接以获取maven生命周期的解释:http://www.tutorialspoint.com/maven/maven_build_life_cycle.htm – ewanc

回答

1

我设法做一个插件,并且还发布了它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> 

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 = "[email protected]") 
    private String password; 

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

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

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

    private void addSlashToDirectory() { 
    if (propertiesDirectory.equals("") || propertiesDirectory.endsWith("/")) return; 
    propertiesDirectory = propertiesDirectory + File.separator; 
    } 

    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; 
    } 
} 

而且在使用插件项目:

<properties> 
    <nameLength>16</nameLength> 
    <password>[email protected]</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> 
相关问题