Search in sources :

Example 11 with IMaven

use of org.eclipse.m2e.core.embedder.IMaven in project liferay-ide by liferay.

the class MavenProjectBuilder method execJarMojo.

public IStatus execJarMojo(IMavenProjectFacade projectFacade, IProgressMonitor monitor) throws CoreException {
    IStatus retval = null;
    ICallable<IStatus> callable = new ICallable<IStatus>() {

        public IStatus call(IMavenExecutionContext context, IProgressMonitor monitor) throws CoreException {
            MavenProject mavenProject = projectFacade.getMavenProject();
            if (mavenProject == null) {
                mavenProject = projectFacade.getMavenProject(monitor);
            }
            IMaven maven = MavenPlugin.getMaven();
            MavenExecutionPlan plan = maven.calculateExecutionPlan(mavenProject, Arrays.asList("jar:jar"), true, monitor);
            List<MojoExecution> mojoExecutions = plan.getMojoExecutions();
            if (mojoExecutions != null) {
                for (MojoExecution mojoExecution : mojoExecutions) {
                    MavenPlugin.getMaven().execute(mavenProject, mojoExecution, monitor);
                }
            }
            return Status.OK_STATUS;
        }
    };
    retval = executeMaven(projectFacade, callable, monitor);
    return retval;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) IMavenExecutionContext(org.eclipse.m2e.core.embedder.IMavenExecutionContext) ICallable(org.eclipse.m2e.core.embedder.ICallable) MavenExecutionPlan(org.apache.maven.lifecycle.MavenExecutionPlan) IMaven(org.eclipse.m2e.core.embedder.IMaven)

Example 12 with IMaven

use of org.eclipse.m2e.core.embedder.IMaven in project m2e-nar by maven-nar.

the class MavenUtils method readSettings.

public static <T extends AbstractCompileMojo> NarExecution readSettings(final ConfiguratorContext context, final IMavenProjectFacade facade, final MojoExecution compileExecution, final Class<T> mojoType, final String buildType, final IProgressMonitor monitor) throws CoreException {
    final IMaven maven = context.getMaven();
    final MavenProject mavenProject = facade.getMavenProject();
    NarExecution settings = null;
    if (compileExecution != null) {
        // Load plugin with Maven in order to check config
        // and to get at aol.properties resource inside the plugin
        AbstractMojo narMojo = loadMojo(maven, mavenProject, compileExecution, AbstractMojo.class, monitor);
        try {
            // ClassRealm pluginRealm =
            // compileExecution.getMojoDescriptor().getPluginDescriptor().getClassRealm();
            // NarClassloader classloader = new NarClassloader(pluginRealm);
            // INarExecutionBuilder builder =
            // classloader.createNarExecutionBuilder(mavenProject,
            // compileMojo);
            // settings = builder.build(NarExecution.MAIN);
            T compileMojo = getConfiguredMojo(maven, mavenProject, compileExecution, mojoType, narMojo.getLog(), monitor);
            compileMojo.setNarProperties(new NarProperties(mavenProject, narMojo.getClass()));
            // Need to call validate to set up defaults
            compileMojo.validate();
            // Resolve the NAR artifacts, possibly from workspace
            compileMojo.prepareNarArtifacts(context, facade, monitor);
            NarExecutionBuilder builder = new NarExecutionBuilder(compileMojo, compileExecution);
            settings = builder.build(buildType);
        } catch (MojoFailureException e) {
            throw new CoreException(new Status(IStatus.ERROR, MavenNarPlugin.PLUGIN_ID, "Couldn't configure mojo"));
        } catch (MojoExecutionException e) {
            throw new CoreException(new Status(IStatus.ERROR, MavenNarPlugin.PLUGIN_ID, "Couldn't configure mojo"));
        } finally {
            releaseMojo(maven, mavenProject, narMojo, compileExecution, monitor);
        }
    } else {
        throw new CoreException(new Status(IStatus.ERROR, MavenNarPlugin.PLUGIN_ID, "Couldn't find default-nar-compile execution"));
    }
    return settings;
}
Also used : NarProperties(com.github.maven_nar.NarProperties) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) MavenProject(org.apache.maven.project.MavenProject) CoreException(org.eclipse.core.runtime.CoreException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AbstractMojo(org.apache.maven.plugin.AbstractMojo) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IMaven(org.eclipse.m2e.core.embedder.IMaven) NarExecution(com.github.sdedwards.m2e_nar.internal.model.NarExecution)

Example 13 with IMaven

use of org.eclipse.m2e.core.embedder.IMaven in project bndtools by bndtools.

the class BndConfigurator method execJarMojo.

private void execJarMojo(final IMavenProjectFacade projectFacade, IProgressMonitor monitor) throws CoreException {
    final IMaven maven = MavenPlugin.getMaven();
    ProjectRegistryManager projectRegistryManager = MavenPluginActivator.getDefault().getMavenProjectManagerImpl();
    ResolverConfiguration resolverConfiguration = new ResolverConfiguration();
    resolverConfiguration.setResolveWorkspaceProjects(true);
    IMavenExecutionContext context = projectRegistryManager.createExecutionContext(projectFacade.getPom(), resolverConfiguration);
    context.execute(new ICallable<Void>() {

        @Override
        public Void call(IMavenExecutionContext context, IProgressMonitor monitor) throws CoreException {
            SubMonitor progress = SubMonitor.convert(monitor);
            MavenProject mavenProject = getMavenProject(projectFacade, progress.newChild(1));
            MavenExecutionPlan plan = maven.calculateExecutionPlan(mavenProject, Arrays.asList("jar:jar"), true, monitor);
            List<MojoExecution> mojoExecutions = plan.getMojoExecutions();
            if (mojoExecutions != null) {
                for (MojoExecution mojoExecution : mojoExecutions) {
                    maven.execute(mavenProject, mojoExecution, progress.newChild(1));
                }
            }
            // We can now decorate based on the build we just did.
            try {
                IProjectDecorator decorator = Injector.ref.get();
                if (decorator != null) {
                    BndProjectInfo info = new MavenProjectInfo(mavenProject);
                    decorator.updateDecoration(projectFacade.getProject(), info);
                }
            } catch (Exception e) {
                logger.logError("Failed to decorate project " + projectFacade.getProject().getName(), e);
            }
            return null;
        }
    }, monitor);
}
Also used : ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) SubMonitor(org.eclipse.core.runtime.SubMonitor) IProjectDecorator(org.bndtools.build.api.IProjectDecorator) CoreException(org.eclipse.core.runtime.CoreException) BndProjectInfo(org.bndtools.build.api.IProjectDecorator.BndProjectInfo) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) ProjectRegistryManager(org.eclipse.m2e.core.internal.project.registry.ProjectRegistryManager) IMavenExecutionContext(org.eclipse.m2e.core.embedder.IMavenExecutionContext) ArrayList(java.util.ArrayList) List(java.util.List) MavenExecutionPlan(org.apache.maven.lifecycle.MavenExecutionPlan) IMaven(org.eclipse.m2e.core.embedder.IMaven)

Example 14 with IMaven

use of org.eclipse.m2e.core.embedder.IMaven in project bndtools by bndtools.

the class IndexConfigurator method getBuildParticipant.

/**
 * We have to temporarily override the ongoing maven build by creating a new context. This allows us to replace the
 * workspace repository with our own.
 */
@Override
public AbstractBuildParticipant getBuildParticipant(final IMavenProjectFacade projectFacade, MojoExecution execution, final IPluginExecutionMetadata executionMetadata) {
    return new MojoExecutionBuildParticipant(execution, true, false) {

        @Override
        public Set<IProject> build(int kind, IProgressMonitor monitor) throws Exception {
            if (appliesToBuildKind(kind)) {
                final IProject project = projectFacade.getProject();
                IMarker[] imarkers = project.findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, false, IResource.DEPTH_INFINITE);
                if (imarkers != null && Arrays.stream(imarkers).map(m -> m.getAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO)).anyMatch(s -> s == IMarker.SEVERITY_ERROR)) {
                    // there are compile errors, don't index
                    return null;
                }
                final SubMonitor progress = SubMonitor.convert(monitor, "Executing indexer plugin", 2);
                final IMaven maven = MavenPlugin.getMaven();
                IMavenExecutionContext context = maven.createExecutionContext();
                context.getExecutionRequest().setWorkspaceReader(new IndexerWorkspaceRepository());
                final MavenProject mavenProject = getMavenProject(projectFacade, progress.newChild(1));
                context.execute(new ICallable<Void>() {

                    @Override
                    public Void call(IMavenExecutionContext context, IProgressMonitor monitor) throws CoreException {
                        maven.execute(mavenProject, getMojoExecution(), monitor);
                        IPath buildDirPath = Path.fromOSString(mavenProject.getBuild().getDirectory());
                        IProject project = projectFacade.getProject();
                        IPath projectPath = project.getLocation();
                        IPath relativeBuildDirPath = buildDirPath.makeRelativeTo(projectPath);
                        IFolder buildDir = project.getFolder(relativeBuildDirPath);
                        buildDir.refreshLocal(IResource.DEPTH_INFINITE, progress.newChild(1));
                        return null;
                    }
                }, progress.newChild(1));
            }
            return null;
        }
    };
}
Also used : ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) Arrays(java.util.Arrays) IFolder(org.eclipse.core.resources.IFolder) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) AbstractBuildParticipant(org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant) SubMonitor(org.eclipse.core.runtime.SubMonitor) AbstractProjectConfigurator(org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator) Exceptions(aQute.lib.exceptions.Exceptions) HashMap(java.util.HashMap) CoreException(org.eclipse.core.runtime.CoreException) ArrayList(java.util.ArrayList) IStatus(org.eclipse.core.runtime.IStatus) IProject(org.eclipse.core.resources.IProject) MavenProject(org.apache.maven.project.MavenProject) IPath(org.eclipse.core.runtime.IPath) LocalArtifactRepository(org.apache.maven.repository.LocalArtifactRepository) IResourceDelta(org.eclipse.core.resources.IResourceDelta) Map(java.util.Map) Artifact(org.apache.maven.artifact.Artifact) IMarker(org.eclipse.core.resources.IMarker) WorkspaceRepository(org.eclipse.aether.repository.WorkspaceRepository) MojoExecutionKey(org.eclipse.m2e.core.project.configurator.MojoExecutionKey) Job(org.eclipse.core.runtime.jobs.Job) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IPluginExecutionMetadata(org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata) FULL_BUILD(org.eclipse.core.resources.IncrementalProjectBuilder.FULL_BUILD) MojoExecution(org.apache.maven.plugin.MojoExecution) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) MojoExecutionBuildParticipant(org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant) POST_BUILD(org.eclipse.core.resources.IResourceChangeEvent.POST_BUILD) File(java.io.File) WorkspaceReader(org.eclipse.aether.repository.WorkspaceReader) IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IJavaModelMarker(org.eclipse.jdt.core.IJavaModelMarker) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) List(java.util.List) IMavenExecutionContext(org.eclipse.m2e.core.embedder.IMavenExecutionContext) ICallable(org.eclipse.m2e.core.embedder.ICallable) IResource(org.eclipse.core.resources.IResource) Path(org.eclipse.core.runtime.Path) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) MavenPlugin(org.eclipse.m2e.core.MavenPlugin) ProjectConfigurationRequest(org.eclipse.m2e.core.project.configurator.ProjectConfigurationRequest) IMaven(org.eclipse.m2e.core.embedder.IMaven) IPath(org.eclipse.core.runtime.IPath) SubMonitor(org.eclipse.core.runtime.SubMonitor) IProject(org.eclipse.core.resources.IProject) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MavenProject(org.apache.maven.project.MavenProject) CoreException(org.eclipse.core.runtime.CoreException) IMavenExecutionContext(org.eclipse.m2e.core.embedder.IMavenExecutionContext) IMarker(org.eclipse.core.resources.IMarker) IMaven(org.eclipse.m2e.core.embedder.IMaven) MojoExecutionBuildParticipant(org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

IMaven (org.eclipse.m2e.core.embedder.IMaven)14 CoreException (org.eclipse.core.runtime.CoreException)9 MavenProject (org.apache.maven.project.MavenProject)7 File (java.io.File)6 IStatus (org.eclipse.core.runtime.IStatus)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 MojoExecution (org.apache.maven.plugin.MojoExecution)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 MavenExecutionPlan (org.apache.maven.lifecycle.MavenExecutionPlan)4 Model (org.apache.maven.model.Model)4 IFile (org.eclipse.core.resources.IFile)4 SubMonitor (org.eclipse.core.runtime.SubMonitor)3 IMavenExecutionContext (org.eclipse.m2e.core.embedder.IMavenExecutionContext)3 NewLiferayProfile (com.liferay.ide.project.core.model.NewLiferayProfile)2 IOException (java.io.IOException)2 Dependency (org.apache.maven.model.Dependency)2 Profile (org.apache.maven.settings.Profile)2 VersionRangeResolutionException (org.eclipse.aether.resolution.VersionRangeResolutionException)2 Version (org.eclipse.aether.version.Version)2