Skip to content

Commit 95594e5

Browse files
Inject MavenSession into Mojo
- use inject for MavenSession - simple tests cleanups
1 parent 20c94ca commit 95594e5

11 files changed

Lines changed: 76 additions & 81 deletions

File tree

src/main/java/org/apache/maven/plugins/war/AbstractWarMojo.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,6 @@ public abstract class AbstractWarMojo extends AbstractMojo {
278278
@Parameter
279279
private List<String> nonFilteredFileExtensions;
280280

281-
/**
282-
* @since 2.1-alpha-2
283-
*/
284-
@Parameter(defaultValue = "${session}", readonly = true, required = true)
285-
private MavenSession session;
286-
287281
/**
288282
* To filter deployment descriptors. <b>Disabled by default.</b>
289283
*
@@ -379,15 +373,19 @@ public abstract class AbstractWarMojo extends AbstractMojo {
379373

380374
private final MavenResourcesFiltering mavenResourcesFiltering;
381375

376+
private final MavenSession session;
377+
382378
protected AbstractWarMojo(
383379
ArtifactHandlerManager artifactHandlerManager,
384380
ArchiverManager archiverManager,
385381
MavenFileFilter mavenFileFilter,
386-
MavenResourcesFiltering mavenResourcesFiltering) {
382+
MavenResourcesFiltering mavenResourcesFiltering,
383+
MavenSession session) {
387384
this.artifactHandlerManager = artifactHandlerManager;
388385
this.archiverManager = archiverManager;
389386
this.mavenFileFilter = mavenFileFilter;
390387
this.mavenResourcesFiltering = mavenResourcesFiltering;
388+
this.session = session;
391389
try {
392390
this.jarArchiver = (JarArchiver) archiverManager.getArchiver("jar");
393391
} catch (NoSuchArchiverException e) {

src/main/java/org/apache/maven/plugins/war/WarExplodedMojo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import javax.inject.Inject;
2222

2323
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
24+
import org.apache.maven.execution.MavenSession;
2425
import org.apache.maven.plugin.MojoExecutionException;
2526
import org.apache.maven.plugin.MojoFailureException;
2627
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -44,8 +45,9 @@ public WarExplodedMojo(
4445
ArtifactHandlerManager artifactHandlerManager,
4546
ArchiverManager archiverManager,
4647
MavenFileFilter mavenFileFilter,
47-
MavenResourcesFiltering mavenResourcesFiltering) {
48-
super(artifactHandlerManager, archiverManager, mavenFileFilter, mavenResourcesFiltering);
48+
MavenResourcesFiltering mavenResourcesFiltering,
49+
MavenSession session) {
50+
super(artifactHandlerManager, archiverManager, mavenFileFilter, mavenResourcesFiltering, session);
4951
}
5052

5153
@Override

src/main/java/org/apache/maven/plugins/war/WarInPlaceMojo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import javax.inject.Inject;
2222

2323
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
24+
import org.apache.maven.execution.MavenSession;
2425
import org.apache.maven.plugin.MojoExecutionException;
2526
import org.apache.maven.plugin.MojoFailureException;
2627
import org.apache.maven.plugins.annotations.Mojo;
@@ -40,8 +41,9 @@ public WarInPlaceMojo(
4041
ArtifactHandlerManager artifactHandlerManager,
4142
ArchiverManager archiverManager,
4243
MavenFileFilter mavenFileFilter,
43-
MavenResourcesFiltering mavenResourcesFiltering) {
44-
super(artifactHandlerManager, archiverManager, mavenFileFilter, mavenResourcesFiltering);
44+
MavenResourcesFiltering mavenResourcesFiltering,
45+
MavenSession session) {
46+
super(artifactHandlerManager, archiverManager, mavenFileFilter, mavenResourcesFiltering, session);
4547
}
4648

4749
@Override

src/main/java/org/apache/maven/plugins/war/WarMojo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.maven.artifact.Artifact;
3333
import org.apache.maven.artifact.DependencyResolutionRequiredException;
3434
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
35+
import org.apache.maven.execution.MavenSession;
3536
import org.apache.maven.plugin.MojoExecutionException;
3637
import org.apache.maven.plugin.MojoFailureException;
3738
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -136,8 +137,9 @@ public WarMojo(
136137
ArchiverManager archiverManager,
137138
MavenFileFilter mavenFileFilter,
138139
MavenResourcesFiltering mavenResourcesFiltering,
139-
MavenProjectHelper projectHelper) {
140-
super(artifactHandlerManager, archiverManager, mavenFileFilter, mavenResourcesFiltering);
140+
MavenProjectHelper projectHelper,
141+
MavenSession session) {
142+
super(artifactHandlerManager, archiverManager, mavenFileFilter, mavenResourcesFiltering, session);
141143
this.projectHelper = projectHelper;
142144
}
143145

src/test/java/org/apache/maven/plugins/war/AbstractWarExplodedMojoTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.Arrays;
2626
import java.util.Collections;
27-
import java.util.LinkedList;
2827
import java.util.List;
2928

3029
import org.apache.maven.plugin.testing.stubs.ArtifactStub;
@@ -94,7 +93,7 @@ protected File setUpMojo(final String testId, ArtifactStub[] artifactStubs, Stri
9493
}
9594
}
9695

97-
configureMojo(mojo, new LinkedList<>(), classesDir, webAppSource, webAppDirectory, project);
96+
configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
9897
setVariableValueToObject(mojo, "workDirectory", workDirectory);
9998

10099
return webAppDirectory;

src/test/java/org/apache/maven/plugins/war/AbstractWarMojoTest.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.File;
2222
import java.io.IOException;
2323
import java.util.Date;
24-
import java.util.List;
2524

2625
import org.apache.maven.execution.DefaultMavenExecutionRequest;
2726
import org.apache.maven.execution.MavenExecutionRequest;
@@ -30,7 +29,6 @@
3029
import org.apache.maven.plugin.testing.stubs.ArtifactStub;
3130
import org.apache.maven.plugins.war.stub.MavenProjectBasicStub;
3231
import org.apache.maven.plugins.war.stub.WarOverlayStub;
33-
import org.apache.maven.shared.filtering.MavenFileFilter;
3432
import org.codehaus.plexus.PlexusContainer;
3533
import org.codehaus.plexus.archiver.ArchiverException;
3634
import org.codehaus.plexus.archiver.jar.JarArchiver;
@@ -47,35 +45,30 @@ public abstract class AbstractWarMojoTest extends AbstractMojoTestCase {
4745

4846
protected abstract File getTestDirectory() throws Exception;
4947

48+
protected void setUp() throws Exception {
49+
super.setUp();
50+
51+
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
52+
.setSystemProperties(System.getProperties())
53+
.setStartTime(new Date());
54+
55+
MavenSession mavenSession =
56+
new MavenSession((PlexusContainer) null, (RepositorySystemSession) null, request, null);
57+
getContainer().addComponent(mavenSession, MavenSession.class.getName());
58+
}
5059
/**
5160
* initialize required parameters
5261
*
5362
* @param mojo The mojo to be tested.
54-
* @param filters The list of filters.
5563
* @param classesDir The classes' directory.
5664
* @param webAppSource The webAppSource.
5765
* @param webAppDir The webAppDir folder.
5866
* @param project The Maven project.
5967
* @throws Exception in case of errors
6068
*/
6169
protected void configureMojo(
62-
AbstractWarMojo mojo,
63-
List<String> filters,
64-
File classesDir,
65-
File webAppSource,
66-
File webAppDir,
67-
MavenProjectBasicStub project)
70+
AbstractWarMojo mojo, File classesDir, File webAppSource, File webAppDir, MavenProjectBasicStub project)
6871
throws Exception {
69-
setVariableValueToObject(mojo, "filters", filters);
70-
setVariableValueToObject(mojo, "mavenFileFilter", lookup(MavenFileFilter.class.getName()));
71-
72-
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
73-
.setSystemProperties(System.getProperties())
74-
.setStartTime(new Date());
75-
76-
MavenSession mavenSession =
77-
new MavenSession((PlexusContainer) null, (RepositorySystemSession) null, request, null);
78-
setVariableValueToObject(mojo, "session", mavenSession);
7972
setVariableValueToObject(mojo, "outdatedCheckPath", "WEB-INF/lib/");
8073
mojo.setClassesDirectory(classesDir);
8174
mojo.setWarSourceDirectory(webAppSource);

src/test/java/org/apache/maven/plugins/war/WarExplodedMojoFilteringTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.LinkedList;
4444
import java.util.List;
4545

46+
import org.apache.maven.execution.MavenSession;
4647
import org.apache.maven.plugins.war.stub.MavenProjectBasicStub;
4748
import org.apache.maven.plugins.war.stub.ResourceStub;
4849
import org.codehaus.plexus.util.FileUtils;
@@ -96,14 +97,16 @@ public void testExplodedWarWithResourceFiltering() throws Exception {
9697
FileUtils.fileWrite(sampleResourceWDir.getAbsolutePath(), content);
9798
FileUtils.fileWrite(sampleResource.getAbsolutePath(), content);
9899

99-
System.setProperty("system.property", "system-property-value");
100+
lookup(MavenSession.class).getSystemProperties().setProperty("system.property", "system-property-value");
100101

101102
// configure mojo
102103
project.addProperty("is_this_simple", "i_think_so");
103104
resources[0].setDirectory(webAppResource.getAbsolutePath());
104105
resources[0].setFiltering(true);
105-
this.configureMojo(mojo, filterList, classesDir, webAppSource, webAppDirectory, project);
106+
this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
106107
setVariableValueToObject(mojo, "webResources", resources);
108+
setVariableValueToObject(mojo, "filters", filterList);
109+
107110
mojo.execute();
108111

109112
// validate operation
@@ -151,8 +154,8 @@ public void testExplodedWarWithResourceFiltering() throws Exception {
151154
reader.readLine());
152155

153156
// update property, and generate again
154-
System.setProperty("system.property", "new-system-property-value");
155-
this.configureMojo(mojo, filterList, classesDir, webAppSource, webAppDirectory, project);
157+
lookup(MavenSession.class).getSystemProperties().setProperty("system.property", "new-system-property-value");
158+
this.configureMojo(mojo, classesDir, webAppSource, webAppDirectory, project);
156159

157160
mojo.execute();
158161

0 commit comments

Comments
 (0)