maven-plugin-testing-harness session.getLocalRepository() returns 空
maven-plugin-testing-harness session.getLocalRepository() returns null
使用 MojoRule 时,会话中的 localRepository 为空,即使我在测试 pom 中传递它也是如此
测试 Pom
<build>
<plugins>
<plugin>
<groupId>myplugin</groupId>
<artifactId>isolatedFeatureBranch</artifactId>
<goals>
<goal>SetRepositoryProperties</goal>
</goals>
<configuration>
<localRepository>${localRepository}</localRepository>
<branchName>feature/defaultInPomFile</branchName>
</configuration>
</plugin>
莫霍Class
/**
* Maven Project Access
*/
@Component
protected MavenProject project;
/**
* Local Repository.
*/
@Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
private ArtifactRepository localRepository;
测试代码
@Rule public TestName name = new TestName();
@Rule public MojoRule mojoRule = new MojoRule();
@Rule public TestResources testResources = new TestResources(testProjects.getAbsolutePath(), workDir.getAbsolutePath());
private MavenProject project;
private MavenSession session;
private SetRepositoryPropertiesMojo mojo;
@Before
public void setUp() throws Exception {
// setup with pom set BRANCHNAME set in pom
File pomDir = testResources.getBasedir("SetPropertiesTestsDefaultInPom");
project = mojoRule.readMavenProject(pomDir);
session = mojoRule.newMavenSession(project);
// Generate Execution and Mojo for testing
MojoExecution execution = mojoRule.newMojoExecution("SetRepositoryProperties");
mojo = (SetRepositoryPropertiesMojo) mojoRule.lookupConfiguredMojo(session, execution);
}
错误
java.lang.NullPointerException
at SetPropertiesFeatureBranchTests.setRemoteRepositoryHappyPath(SetPropertiesFeatureBranchTests.java:98)
我们需要创建一个localRepository,然后使用请求将其附加到会话中。之后,我们创建 mojo 并且 class 的 localRepository 变量包含 repo
测试代码
public void setUp() throws Exception {
// setup with pom set BRANCHNAME set in pom
File pomDir = testResources.getBasedir("SetPropertiesTestsDefaultInPom");
project = mojoRule.readMavenProject(pomDir);
// Generate session
session = mojoRule.newMavenSession(project);
// add localRepo - framework doesn't do this on its own
ArtifactRepository localRepo = createLocalArtifactRepository();
session.getRequest().setLocalRepository(localRepo);
// Generate Execution and Mojo for testing
MojoExecution execution = mojoRule.newMojoExecution("SetRepositoryProperties");
mojo = (SetRepositoryPropertiesMojo) mojoRule.lookupConfiguredMojo(session, execution);
}
/**
* Generate a local repository
* @return local repository object
*/
private ArtifactRepository createLocalArtifactRepository() {
return new MavenArtifactRepository("local",
localRepoDir.toURI().toString(),
new DefaultRepositoryLayout(),
new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE ),
new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE )
);
}
使用 MojoRule 时,会话中的 localRepository 为空,即使我在测试 pom 中传递它也是如此
测试 Pom
<build>
<plugins>
<plugin>
<groupId>myplugin</groupId>
<artifactId>isolatedFeatureBranch</artifactId>
<goals>
<goal>SetRepositoryProperties</goal>
</goals>
<configuration>
<localRepository>${localRepository}</localRepository>
<branchName>feature/defaultInPomFile</branchName>
</configuration>
</plugin>
莫霍Class
/**
* Maven Project Access
*/
@Component
protected MavenProject project;
/**
* Local Repository.
*/
@Parameter( defaultValue = "${localRepository}", readonly = true, required = true )
private ArtifactRepository localRepository;
测试代码
@Rule public TestName name = new TestName();
@Rule public MojoRule mojoRule = new MojoRule();
@Rule public TestResources testResources = new TestResources(testProjects.getAbsolutePath(), workDir.getAbsolutePath());
private MavenProject project;
private MavenSession session;
private SetRepositoryPropertiesMojo mojo;
@Before
public void setUp() throws Exception {
// setup with pom set BRANCHNAME set in pom
File pomDir = testResources.getBasedir("SetPropertiesTestsDefaultInPom");
project = mojoRule.readMavenProject(pomDir);
session = mojoRule.newMavenSession(project);
// Generate Execution and Mojo for testing
MojoExecution execution = mojoRule.newMojoExecution("SetRepositoryProperties");
mojo = (SetRepositoryPropertiesMojo) mojoRule.lookupConfiguredMojo(session, execution);
}
错误
java.lang.NullPointerException
at SetPropertiesFeatureBranchTests.setRemoteRepositoryHappyPath(SetPropertiesFeatureBranchTests.java:98)
我们需要创建一个localRepository,然后使用请求将其附加到会话中。之后,我们创建 mojo 并且 class 的 localRepository 变量包含 repo
测试代码
public void setUp() throws Exception {
// setup with pom set BRANCHNAME set in pom
File pomDir = testResources.getBasedir("SetPropertiesTestsDefaultInPom");
project = mojoRule.readMavenProject(pomDir);
// Generate session
session = mojoRule.newMavenSession(project);
// add localRepo - framework doesn't do this on its own
ArtifactRepository localRepo = createLocalArtifactRepository();
session.getRequest().setLocalRepository(localRepo);
// Generate Execution and Mojo for testing
MojoExecution execution = mojoRule.newMojoExecution("SetRepositoryProperties");
mojo = (SetRepositoryPropertiesMojo) mojoRule.lookupConfiguredMojo(session, execution);
}
/**
* Generate a local repository
* @return local repository object
*/
private ArtifactRepository createLocalArtifactRepository() {
return new MavenArtifactRepository("local",
localRepoDir.toURI().toString(),
new DefaultRepositoryLayout(),
new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE ),
new ArtifactRepositoryPolicy( true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE )
);
}