gpt4 book ai didi

java - Maven运行测试,然后编译然后进行其他测试

转载 作者:行者123 更新时间:2023-12-01 09:59:21 24 4
gpt4 key购买 nike

我的目标是让 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>

我读过一些关于处决的帖子,但到目前为止还没有成功。

更新:
我设法制作了一个插件。请参阅下面我的回答。

最佳答案

我成功制作了一个插件并发布了它on my github account 。我的 Mojo 的 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(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(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>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>

关于java - Maven运行测试,然后编译然后进行其他测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36941491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com