Search in sources :

Example 1 with BuildPluginManager

use of org.apache.maven.plugin.BuildPluginManager in project m2e-code-quality by m2e-code-quality.

the class AbstractMavenPluginProjectConfigurator method getResourceResolver.

public static ResourceResolver getResourceResolver(final MojoExecution mojoExecution, final MavenSession session, final IPath projectLocation) throws CoreException {
    // call for side effect of ensuring that the realm is set in the
    // descriptor.
    final IMaven mvn = MavenPlugin.getMaven();
    final List<IPath> pluginDepencyProjectLocations = new ArrayList<>();
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    final IMavenProjectRegistry mavenProjectRegistry = MavenPlugin.getMavenProjectRegistry();
    final IMavenProjectFacade[] projects = mavenProjectRegistry.getProjects();
    final List<Dependency> dependencies = mojoExecution.getPlugin().getDependencies();
    for (final Dependency dependency : dependencies) {
        for (final IMavenProjectFacade projectFacade : projects) {
            final IProject project = projectFacade.getProject();
            if (!project.isAccessible()) {
                LOG.debug("Project registry contains closed project {}", project);
                // logic, closed projects should not be there
                continue;
            }
            final ArtifactKey artifactKey = projectFacade.getArtifactKey();
            if (artifactKey.getGroupId().equals(dependency.getGroupId()) && artifactKey.getArtifactId().equals(dependency.getArtifactId()) && artifactKey.getVersion().equals(dependency.getVersion())) {
                final IResource outputLocation = root.findMember(projectFacade.getOutputLocation());
                if (outputLocation != null) {
                    pluginDepencyProjectLocations.add(outputLocation.getLocation());
                }
            }
        }
    }
    try {
        // we want just the classpath of the Mojo to load resources from it
        BuildPluginManager buildPluginManager = ((MavenImpl) mvn).lookupComponent(BuildPluginManager.class);
        ClassRealm pluginRealm = buildPluginManager.getPluginRealm(session, mojoExecution.getMojoDescriptor().getPluginDescriptor());
        return new ResourceResolver(pluginRealm, projectLocation, pluginDepencyProjectLocations);
    } catch (PluginResolutionException | PluginManagerException e) {
        throw new CoreException(new Status(IStatus.ERROR, FrameworkUtil.getBundle(AbstractMavenPluginProjectConfigurator.class).getSymbolicName(), "Failed to access classpath of mojo " + mojoExecution.getMojoDescriptor().getId(), e));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) PluginResolutionException(org.apache.maven.plugin.PluginResolutionException) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) Dependency(org.apache.maven.model.Dependency) IMavenProjectRegistry(org.eclipse.m2e.core.project.IMavenProjectRegistry) IProject(org.eclipse.core.resources.IProject) ClassRealm(org.codehaus.plexus.classworlds.realm.ClassRealm) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) MavenImpl(org.eclipse.m2e.core.internal.embedder.MavenImpl) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) BuildPluginManager(org.apache.maven.plugin.BuildPluginManager) PluginManagerException(org.apache.maven.plugin.PluginManagerException) IMaven(org.eclipse.m2e.core.embedder.IMaven) IResource(org.eclipse.core.resources.IResource)

Example 2 with BuildPluginManager

use of org.apache.maven.plugin.BuildPluginManager in project vertx-maven-plugin by reactiverse.

the class MojoUtils method compile.

/**
 * Execute the Maven Compiler Plugin to compile java sources.
 *
 * @param project            the project
 * @param mavenSession       the session
 * @param buildPluginManager the build plugin manager
 * @throws Exception if the compilation fails for any reason
 */
public static void compile(MavenProject project, MavenSession mavenSession, BuildPluginManager buildPluginManager) throws Exception {
    Optional<Plugin> mvnCompilerPlugin = project.getBuildPlugins().stream().filter(plugin -> A_MAVEN_COMPILER_PLUGIN.equals(plugin.getArtifactId())).findFirst();
    String pluginVersion = properties.getProperty(V_MAVEN_COMPILER_PLUGIN);
    if (mvnCompilerPlugin.isPresent()) {
        pluginVersion = mvnCompilerPlugin.get().getVersion();
    }
    Optional<Xpp3Dom> optConfiguration = buildConfiguration(project, A_MAVEN_COMPILER_PLUGIN, GOAL_COMPILE);
    if (optConfiguration.isPresent()) {
        Xpp3Dom configuration = optConfiguration.get();
        executeMojo(plugin(G_MAVEN_COMPILER_PLUGIN, A_MAVEN_COMPILER_PLUGIN, pluginVersion), goal(GOAL_COMPILE), configuration, executionEnvironment(project, mavenSession, buildPluginManager));
    }
}
Also used : BuildPluginManager(org.apache.maven.plugin.BuildPluginManager) PluginExecution(org.apache.maven.model.PluginExecution) MavenSession(org.apache.maven.execution.MavenSession) Properties(java.util.Properties) URL(java.net.URL) Dependency(org.apache.maven.model.Dependency) IOException(java.io.IOException) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Objects(java.util.Objects) Plugin(org.apache.maven.model.Plugin) List(java.util.List) MavenProject(org.apache.maven.project.MavenProject) Optional(java.util.Optional) MojoExecutor(org.twdata.maven.mojoexecutor.MojoExecutor) InputStream(java.io.InputStream) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Plugin(org.apache.maven.model.Plugin)

Aggregations

Dependency (org.apache.maven.model.Dependency)2 BuildPluginManager (org.apache.maven.plugin.BuildPluginManager)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 MavenSession (org.apache.maven.execution.MavenSession)1 Plugin (org.apache.maven.model.Plugin)1 PluginExecution (org.apache.maven.model.PluginExecution)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 PluginManagerException (org.apache.maven.plugin.PluginManagerException)1 PluginResolutionException (org.apache.maven.plugin.PluginResolutionException)1 MavenProject (org.apache.maven.project.MavenProject)1 ClassRealm (org.codehaus.plexus.classworlds.realm.ClassRealm)1 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)1 IProject (org.eclipse.core.resources.IProject)1