Search in sources :

Example 1 with PluginVersionResult

use of org.apache.maven.plugin.version.PluginVersionResult in project maven-plugins by apache.

the class DescribeMojoTest method testLookupPluginDescriptorPrefixWithoutVersion.

public void testLookupPluginDescriptorPrefixWithoutVersion() throws Throwable {
    DescribeMojo mojo = new DescribeMojo();
    PluginInfo pi = new PluginInfo();
    pi.setPrefix("help");
    Plugin plugin = new Plugin();
    plugin.setGroupId("org.test");
    plugin.setArtifactId("test");
    PluginDescriptor pd = new PluginDescriptor();
    MojoDescriptorCreator mojoDescriptorCreator = mock(MojoDescriptorCreator.class);
    PluginVersionResolver pluginVersionResolver = mock(PluginVersionResolver.class);
    MavenPluginManagerHelper pluginManager = mock(MavenPluginManagerHelper.class);
    PluginVersionResult versionResult = mock(PluginVersionResult.class);
    MavenSession session = mock(MavenSession.class);
    PrivateAccessor.setField(mojo, "mojoDescriptorCreator", mojoDescriptorCreator);
    PrivateAccessor.setField(mojo, "pluginVersionResolver", pluginVersionResolver);
    PrivateAccessor.setField(mojo, "pluginManager", pluginManager);
    PrivateAccessor.setField(mojo, "session", session);
    PrivateAccessor.setField(mojo, "project", new MavenProject());
    when(mojoDescriptorCreator.findPluginForPrefix("help", session)).thenReturn(plugin);
    when(pluginVersionResolver.resolve(any(PluginVersionRequest.class))).thenReturn(versionResult);
    when(versionResult.getVersion()).thenReturn("1.0");
    when(pluginManager.getPluginDescriptor(any(Plugin.class), eq(session))).thenReturn(pd);
    PluginDescriptor returned = (PluginDescriptor) PrivateAccessor.invoke(mojo, "lookupPluginDescriptor", new Class[] { PluginInfo.class }, new Object[] { pi });
    assertEquals(pd, returned);
    verify(mojoDescriptorCreator).findPluginForPrefix("help", session);
    ArgumentCaptor<PluginVersionRequest> versionArgument = ArgumentCaptor.forClass(PluginVersionRequest.class);
    verify(pluginVersionResolver).resolve(versionArgument.capture());
    assertEquals("org.test", versionArgument.getValue().getGroupId());
    assertEquals("test", versionArgument.getValue().getArtifactId());
    ArgumentCaptor<Plugin> argument = ArgumentCaptor.forClass(Plugin.class);
    verify(pluginManager).getPluginDescriptor(argument.capture(), eq(session));
    Plugin capturedPlugin = argument.getValue();
    assertEquals("org.test", capturedPlugin.getGroupId());
    assertEquals("test", capturedPlugin.getArtifactId());
    assertEquals("1.0", capturedPlugin.getVersion());
}
Also used : PluginVersionResolver(org.apache.maven.plugin.version.PluginVersionResolver) PluginVersionResult(org.apache.maven.plugin.version.PluginVersionResult) PluginDescriptor(org.apache.maven.plugin.descriptor.PluginDescriptor) MavenSession(org.apache.maven.execution.MavenSession) MavenProject(org.apache.maven.project.MavenProject) MojoDescriptorCreator(org.apache.maven.lifecycle.internal.MojoDescriptorCreator) PluginInfo(org.apache.maven.plugins.help.DescribeMojo.PluginInfo) MavenPluginManagerHelper(org.apache.maven.reporting.exec.MavenPluginManagerHelper) PluginVersionRequest(org.apache.maven.plugin.version.PluginVersionRequest) Plugin(org.apache.maven.model.Plugin)

Example 2 with PluginVersionResult

use of org.apache.maven.plugin.version.PluginVersionResult in project maven-plugins by apache.

the class DescribeMojo method lookupPluginDescriptor.

/**
 * Method for retrieving the description of the plugin
 *
 * @param pi holds information of the plugin whose description is to be retrieved
 * @return a PluginDescriptor where the plugin description is to be retrieved
 * @throws MojoExecutionException if the plugin could not be verify
 * @throws MojoFailureException   if groupId or artifactId is empty
 */
private PluginDescriptor lookupPluginDescriptor(PluginInfo pi) throws MojoExecutionException, MojoFailureException {
    Plugin forLookup = null;
    if (StringUtils.isNotEmpty(pi.getPrefix())) {
        try {
            forLookup = mojoDescriptorCreator.findPluginForPrefix(pi.getPrefix(), session);
        } catch (NoPluginFoundForPrefixException e) {
            throw new MojoExecutionException("Unable to find the plugin with prefix: " + pi.getPrefix(), e);
        }
    } else if (StringUtils.isNotEmpty(pi.getGroupId()) && StringUtils.isNotEmpty(pi.getArtifactId())) {
        forLookup = new Plugin();
        forLookup.setGroupId(pi.getGroupId());
        forLookup.setArtifactId(pi.getArtifactId());
    }
    if (forLookup == null) {
        String msg = "You must specify either: both 'groupId' and 'artifactId' parameters OR a 'plugin' parameter" + " OR a 'cmd' parameter. For instance:" + LS + "  # mvn help:describe -Dcmd=install" + LS + "or" + LS + "  # mvn help:describe -Dcmd=help:describe" + LS + "or" + LS + "  # mvn help:describe -Dplugin=org.apache.maven.plugins:maven-help-plugin" + LS + "or" + LS + "  # mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-help-plugin" + LS + LS + "Try 'mvn help:help -Ddetail=true' for more information.";
        throw new MojoFailureException(msg);
    }
    if (StringUtils.isNotEmpty(pi.getVersion())) {
        forLookup.setVersion(pi.getVersion());
    } else {
        try {
            DefaultPluginVersionRequest versionRequest = new DefaultPluginVersionRequest(forLookup, session);
            versionRequest.setPom(project.getModel());
            PluginVersionResult versionResult = pluginVersionResolver.resolve(versionRequest);
            forLookup.setVersion(versionResult.getVersion());
        } catch (PluginVersionResolutionException e) {
            throw new MojoExecutionException("Unable to resolve the version of the plugin with prefix: " + pi.getPrefix(), e);
        }
    }
    try {
        return pluginManager.getPluginDescriptor(forLookup, session);
    } catch (Exception e) {
        throw new MojoExecutionException("Error retrieving plugin descriptor for:" + LS + LS + "groupId: '" + groupId + "'" + LS + "artifactId: '" + artifactId + "'" + LS + "version: '" + version + "'" + LS + LS, e);
    }
}
Also used : NoPluginFoundForPrefixException(org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException) PluginVersionResolutionException(org.apache.maven.plugin.version.PluginVersionResolutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) PluginVersionResult(org.apache.maven.plugin.version.PluginVersionResult) DefaultPluginVersionRequest(org.apache.maven.plugin.version.DefaultPluginVersionRequest) PluginVersionResolutionException(org.apache.maven.plugin.version.PluginVersionResolutionException) NoPluginFoundForPrefixException(org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Plugin(org.apache.maven.model.Plugin)

Aggregations

Plugin (org.apache.maven.model.Plugin)2 PluginVersionResult (org.apache.maven.plugin.version.PluginVersionResult)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MavenSession (org.apache.maven.execution.MavenSession)1 MojoDescriptorCreator (org.apache.maven.lifecycle.internal.MojoDescriptorCreator)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 PluginDescriptor (org.apache.maven.plugin.descriptor.PluginDescriptor)1 NoPluginFoundForPrefixException (org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException)1 DefaultPluginVersionRequest (org.apache.maven.plugin.version.DefaultPluginVersionRequest)1 PluginVersionRequest (org.apache.maven.plugin.version.PluginVersionRequest)1 PluginVersionResolutionException (org.apache.maven.plugin.version.PluginVersionResolutionException)1 PluginVersionResolver (org.apache.maven.plugin.version.PluginVersionResolver)1 PluginInfo (org.apache.maven.plugins.help.DescribeMojo.PluginInfo)1 MavenProject (org.apache.maven.project.MavenProject)1 MavenPluginManagerHelper (org.apache.maven.reporting.exec.MavenPluginManagerHelper)1