-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
-Added a helper class to test and debug Mojos in an IDE #65
Open
mdproctor
wants to merge
1
commit into
Vertispan:main
Choose a base branch
from
mdproctor:testmojo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,4 +32,4 @@ | |
</plugins> | ||
</pluginManagement> | ||
</build> | ||
</project> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package net.cardosi.mojo; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Method; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.maven.DefaultMaven; | ||
import org.apache.maven.Maven; | ||
import org.apache.maven.execution.DefaultMavenExecutionRequest; | ||
import org.apache.maven.execution.DefaultMavenExecutionResult; | ||
import org.apache.maven.execution.MavenExecutionRequest; | ||
import org.apache.maven.execution.MavenExecutionRequestPopulator; | ||
import org.apache.maven.execution.MavenExecutionResult; | ||
import org.apache.maven.execution.MavenSession; | ||
import org.apache.maven.graph.GraphBuilder; | ||
import org.apache.maven.model.Plugin; | ||
import org.apache.maven.plugin.ContextEnabled; | ||
import org.apache.maven.plugin.Mojo; | ||
import org.apache.maven.plugin.MojoExecution; | ||
import org.apache.maven.plugin.descriptor.MojoDescriptor; | ||
import org.apache.maven.plugin.descriptor.PluginDescriptor; | ||
import org.apache.maven.plugin.testing.AbstractMojoTestCase; | ||
import org.apache.maven.project.MavenProject; | ||
import org.apache.maven.project.ProjectBuilder; | ||
import org.eclipse.aether.DefaultRepositorySystemSession; | ||
import org.eclipse.aether.RepositorySystemSession; | ||
import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; | ||
import org.eclipse.aether.repository.LocalRepository; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
public class MojoTester | ||
extends AbstractMojoTestCase { | ||
|
||
protected void setUp() throws Exception { | ||
// required for mojo lookups to work | ||
super.setUp(); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
MojoTester test = new MojoTester(); | ||
test.setUp(); | ||
test.testMojoGoal(); | ||
} | ||
|
||
protected MavenSession newMavenSession() { | ||
try { | ||
MavenExecutionRequest request = new DefaultMavenExecutionRequest(); | ||
MavenExecutionResult result = new DefaultMavenExecutionResult(); | ||
|
||
// populate sensible defaults, including repository basedir and remote repos | ||
MavenExecutionRequestPopulator populator; | ||
populator = getContainer().lookup( MavenExecutionRequestPopulator.class ); | ||
populator.populateDefaults( request ); | ||
|
||
// this is needed to allow java profiles to get resolved; i.e. avoid during project builds: | ||
// [ERROR] Failed to determine Java version for profile java-1.5-detected @ org.apache.commons:commons-parent:22, /Users/alex/.m2/repository/org/apache/commons/commons-parent/22/commons-parent-22.pom, line 909, column 14 | ||
request.setSystemProperties( System.getProperties() ); | ||
|
||
// and this is needed so that the repo session in the maven session | ||
// has a repo manager, and it points at the local repo | ||
// (cf MavenRepositorySystemUtils.newSession() which is what is otherwise done) | ||
DefaultMaven maven = (DefaultMaven) getContainer().lookup(Maven.class); | ||
DefaultRepositorySystemSession repoSession = | ||
(DefaultRepositorySystemSession) maven.newRepositorySession( request ); | ||
repoSession.setLocalRepositoryManager( | ||
new SimpleLocalRepositoryManagerFactory().newInstance(repoSession, | ||
new LocalRepository(request.getLocalRepository().getBasedir() ))); | ||
|
||
@SuppressWarnings("deprecation") | ||
MavenSession session = new MavenSession( getContainer(), | ||
repoSession, | ||
request, result ); | ||
return session; | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** As {@link #lookupConfiguredMojo(MavenProject, String)} but taking the pom file | ||
* and creating the {@link MavenProject}. */ | ||
protected Mojo lookupConfiguredMojo(File pom, String goal) throws Exception { | ||
assertNotNull( pom ); | ||
assertTrue( pom.exists() ); | ||
|
||
MavenSession session = newMavenSession(); | ||
session.getRequest().setPom(pom); | ||
session.getProjectBuildingRequest().setResolveDependencies(true); | ||
|
||
GraphBuilder gbuilder = getContainer().lookup(GraphBuilder.class); | ||
gbuilder.build(session); | ||
|
||
List<MavenProject> mavenProjects = session.getProjects(); | ||
|
||
ProjectBuilder projectBuilder = lookup(ProjectBuilder.class); | ||
Method method = projectBuilder.getClass().getDeclaredMethod("resolveDependencies", MavenProject.class, RepositorySystemSession.class); | ||
method.setAccessible(true); | ||
|
||
for (MavenProject mavenProject : mavenProjects) { | ||
method.invoke(projectBuilder, mavenProject, session.getRepositorySession()); | ||
} | ||
|
||
final MojoExecution mojoExecution = newMojoExecution(goal); | ||
|
||
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor(); | ||
PluginDescriptor pluginDescriptor = mojoDescriptor.getPluginDescriptor(); | ||
|
||
Plugin plugin = session.getCurrentProject().getBuild().getPluginsAsMap().get(pluginDescriptor.getGroupId() + ":" + pluginDescriptor.getArtifactId()); | ||
pluginDescriptor.setPlugin(plugin); | ||
|
||
AbstractBuildMojo mojo = (AbstractBuildMojo) lookupConfiguredMojo( session, newMojoExecution( goal ) ); | ||
Map<String, Object> pluginContext = mojo.mavenSession.getPluginContext(pluginDescriptor, session.getCurrentProject()); | ||
pluginContext.put( "project", session.getCurrentProject() ); | ||
pluginContext.put( "pluginDescriptor", pluginDescriptor ); | ||
( (ContextEnabled) mojo ).setPluginContext(pluginContext); | ||
|
||
return mojo; | ||
} | ||
|
||
@Test | ||
@Ignore | ||
public void testMojoGoal() throws Exception | ||
{ | ||
File testPom = new File(getBasedir(), | ||
"target/test-classes/hello-world-reactor/pom.xml" ); | ||
|
||
Files.exists(testPom.toPath()); | ||
|
||
final WatchMojo mojo = (WatchMojo) this.lookupConfiguredMojo(testPom, "watch"); | ||
mojo.execute(); | ||
} | ||
|
||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is targetting just that parent pom, why the changes to the transitive-dependencies parent pom?
the next iter will have the actual building code in a plain java jar library (maven agnostic, so you could build some IDE tooling too in theory), and will be used by the mojo - this tooling will be helpful, but we'll be able to actually write plain tests too, and actually edit some temp files and see what happens.