Search in sources :

Example 21 with MojoExecution

use of org.apache.maven.plugin.MojoExecution 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 22 with MojoExecution

use of org.apache.maven.plugin.MojoExecution in project fabric8-maven-plugin by fabric8io.

the class GoalFinder method checkGoalInPhase.

private boolean checkGoalInPhase(MavenProject project, MavenSession session, String goal, String phase) throws MojoExecutionException {
    Lifecycle lifecycle = defaultLifeCycles.get(phase);
    if (lifecycle == null) {
        throw new MojoExecutionException("Cannot find lifecycle phase " + phase);
    }
    LifecycleMappingDelegate delegate = findDelegate(lifecycle);
    try {
        Map<String, List<MojoExecution>> executionsMap = delegate.calculateLifecycleMappings(session, project, lifecycle, phase);
        boolean foundPhase = false;
        boolean foundGoal = false;
        for (String p : lifecycle.getPhases()) {
            List<MojoExecution> executions = executionsMap.get(p);
            if (executions != null) {
                for (MojoExecution execution : executions) {
                    MojoDescriptor desc = execution.getMojoDescriptor();
                    if (desc != null && desc.getFullGoalName().equals(goal)) {
                        foundGoal = true;
                        break;
                    }
                }
            }
            if (phase.equals(p)) {
                foundPhase = true;
                break;
            }
        }
        return foundPhase && foundGoal;
    } catch (Exception e) {
        throw new MojoExecutionException("Interna: Cannot extract executions", e);
    }
}
Also used : MojoDescriptor(org.apache.maven.plugin.descriptor.MojoDescriptor) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoExecution(org.apache.maven.plugin.MojoExecution) Lifecycle(org.apache.maven.lifecycle.Lifecycle) List(java.util.List) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) LifecycleMappingDelegate(org.apache.maven.lifecycle.LifecycleMappingDelegate) DefaultLifecycleMappingDelegate(org.apache.maven.lifecycle.internal.DefaultLifecycleMappingDelegate)

Example 23 with MojoExecution

use of org.apache.maven.plugin.MojoExecution in project m2e-nar by maven-nar.

the class MavenUtils method buildTestCompileNarExecutions.

public static List<NarExecution> buildTestCompileNarExecutions(final ConfiguratorContext context, final IMavenProjectFacade facade, final IProgressMonitor monitor) throws CoreException {
    List<NarExecution> narExecutions = new ArrayList<NarExecution>();
    List<MojoExecution> testCompileExecutions = MavenUtils.getTestCompileExecutions(context, facade, monitor);
    for (MojoExecution testCompileExecution : testCompileExecutions) {
        NarExecution narSettings = MavenUtils.readTestCompileSettings(context, facade, testCompileExecution, monitor);
        narExecutions.add(narSettings);
    }
    return narExecutions;
}
Also used : MojoExecution(org.apache.maven.plugin.MojoExecution) ArrayList(java.util.ArrayList) NarExecution(com.github.sdedwards.m2e_nar.internal.model.NarExecution)

Example 24 with MojoExecution

use of org.apache.maven.plugin.MojoExecution in project antlr4 by tunnelvisionlabs.

the class Antlr4MojoTest method importsCustomLayout.

@Test
public void importsCustomLayout() throws Exception {
    File baseDir = resources.getBasedir("importsCustom");
    File antlrDir = new File(baseDir, "src/main/antlr4");
    File generatedSources = new File(baseDir, "src/main/java");
    File genTestLexer = new File(generatedSources, "foo/TestLexer.java");
    File genTestParser = new File(generatedSources, "foo/TestParser.java");
    File genHello = new File(generatedSources, "foo/HelloParser.java");
    File baseGrammar = new File(antlrDir, "imports/TestBaseLexer.g4");
    File lexerGrammar = new File(antlrDir, "TestLexer.g4");
    File parserGrammar = new File(antlrDir, "TestParser.g4");
    Xpp3Dom outputDirectory = TestMavenRuntime.newParameter("outputDirectory", "src/main/java/foo");
    Xpp3Dom arguments = new Xpp3Dom("arguments");
    arguments.addChild(TestMavenRuntime.newParameter("argument", "-package"));
    arguments.addChild(TestMavenRuntime.newParameter("argument", "foo"));
    MavenProject project = maven.readMavenProject(baseDir);
    MavenSession session = maven.newMavenSession(project);
    MojoExecution exec = maven.newMojoExecution("antlr4", outputDirectory, arguments);
    // //////////////////////////////////////////////////////////////////////
    // 1st - all grammars have to be processed
    // //////////////////////////////////////////////////////////////////////
    assertFalse(genHello.exists());
    assertFalse(genTestParser.exists());
    assertFalse(genTestLexer.exists());
    maven.executeMojo(session, project, exec);
    assertTrue(genHello.isFile());
    assertTrue(genTestParser.isFile());
    assertTrue(genTestLexer.isFile());
    // //////////////////////////////////////////////////////////////////////
    // 2nd - nothing has been modified, no grammars have to be processed
    // //////////////////////////////////////////////////////////////////////
    {
        byte[] testLexerSum = checksum(genTestLexer);
        byte[] testParserSum = checksum(genTestParser);
        byte[] helloSum = checksum(genHello);
        maven.executeMojo(session, project, exec);
        assertTrue(Arrays.equals(testLexerSum, checksum(genTestLexer)));
        assertTrue(Arrays.equals(testParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(helloSum, checksum(genHello)));
    }
    // //////////////////////////////////////////////////////////////////////
    // 3rd - the imported grammar changed, every dependency has to be processed
    // //////////////////////////////////////////////////////////////////////
    // modify the grammar to make checksum comparison detect a change
    Change change = Change.of(baseGrammar, "DOT: '.' ;");
    try {
        byte[] testLexerSum = checksum(genTestLexer);
        byte[] testParserSum = checksum(genTestParser);
        byte[] helloSum = checksum(genHello);
        maven.executeMojo(session, project, exec);
        assertFalse(Arrays.equals(testLexerSum, checksum(genTestLexer)));
        assertFalse(Arrays.equals(testParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(helloSum, checksum(genHello)));
    } finally {
        change.close();
    }
    // //////////////////////////////////////////////////////////////////////
    // 4th - the lexer grammar changed, the parser grammar has to be processed as well
    // //////////////////////////////////////////////////////////////////////
    // modify the grammar to make checksum comparison detect a change
    change = Change.of(lexerGrammar, "fragment DOT : '.';");
    try {
        byte[] testLexerSum = checksum(genTestLexer);
        byte[] testParserSum = checksum(genTestParser);
        byte[] helloSum = checksum(genHello);
        maven.executeMojo(session, project, exec);
        assertFalse(Arrays.equals(testLexerSum, checksum(genTestLexer)));
        assertFalse(Arrays.equals(testParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(helloSum, checksum(genHello)));
    } finally {
        change.close();
    }
    // //////////////////////////////////////////////////////////////////////
    // 5th - the parser grammar changed, no other grammars have to be processed
    // //////////////////////////////////////////////////////////////////////
    // modify the grammar to make checksum comparison detect a change
    change = Change.of(parserGrammar, " t : WS* ;");
    try {
        byte[] testLexerSum = checksum(genTestLexer);
        byte[] testParserSum = checksum(genTestParser);
        byte[] helloSum = checksum(genHello);
        maven.executeMojo(session, project, exec);
        assumeTrue(Arrays.equals(testLexerSum, checksum(genTestLexer)));
        assertFalse(Arrays.equals(testParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(helloSum, checksum(genHello)));
    } finally {
        change.close();
    }
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) File(java.io.File) Test(org.junit.Test)

Example 25 with MojoExecution

use of org.apache.maven.plugin.MojoExecution in project bndtools by bndtools.

the class BndConfigurator method getBuildParticipant.

@Override
public AbstractBuildParticipant getBuildParticipant(final IMavenProjectFacade projectFacade, MojoExecution execution, IPluginExecutionMetadata executionMetadata) {
    return new MojoExecutionBuildParticipant(execution, true, true) {

        @Override
        public Set<IProject> build(int kind, IProgressMonitor monitor) throws Exception {
            // build mojo like normal
            final Set<IProject> build = super.build(kind, monitor);
            // nothing to do if configuration build
            if (kind == AbstractBuildParticipant2.PRECONFIGURE_BUILD) {
                return build;
            }
            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 build jar
                return build;
            }
            // now we make sure jar is built in separate job, doing this during maven builder will throw lifecycle
            // errors
            Job job = new WorkspaceJob("Executing " + project.getName() + " jar:jar goal") {

                @Override
                public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
                    SubMonitor progress = SubMonitor.convert(monitor, 3);
                    execJarMojo(projectFacade, progress.newChild(1, SubMonitor.SUPPRESS_NONE));
                    // Find the maven output directory (usually "target")
                    MavenProject mvnProject = getMavenProject(projectFacade, progress.newChild(1));
                    IPath buildDirPath = Path.fromOSString(mvnProject.getBuild().getDirectory());
                    IPath projectPath = project.getLocation();
                    IPath relativeBuildDirPath = buildDirPath.makeRelativeTo(projectPath);
                    IFolder buildDir = project.getFolder(relativeBuildDirPath);
                    if (buildDir != null) {
                        // TODO: there *may* be a remaining issue here if a source-generation plugin gets triggered
                        // by the above invocation of the jar:jar goal.
                        // This could cause Eclipse to think that the Java sources are dirty and queue the project
                        // for rebuilding, thus entering an infinite loop.
                        // One solution would be to find the output artifact jar and refresh ONLY that. However we
                        // have not been able to create the condition we
                        // are worried about so we are deferring any extra work on this until it's shown to be a
                        // real problem.
                        buildDir.refreshLocal(IResource.DEPTH_INFINITE, progress.newChild(1));
                    } else {
                        Logger.getLogger(BndConfigurator.class).logError(String.format("Project build folder '%s' does not exist, or is not a child of the project path '%s'", buildDirPath, projectPath), null);
                        progress.worked(1);
                    }
                    return Status.OK_STATUS;
                }
            };
            job.setRule(project);
            job.schedule();
            return build;
        }
    };
}
Also used : Arrays(java.util.Arrays) MavenExecutionPlan(org.apache.maven.lifecycle.MavenExecutionPlan) IFolder(org.eclipse.core.resources.IFolder) AbstractBuildParticipant(org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant) SubMonitor(org.eclipse.core.runtime.SubMonitor) AbstractProjectConfigurator(org.eclipse.m2e.core.project.configurator.AbstractProjectConfigurator) CoreException(org.eclipse.core.runtime.CoreException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProjectRegistryManager(org.eclipse.m2e.core.internal.project.registry.ProjectRegistryManager) ArrayList(java.util.ArrayList) ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) IStatus(org.eclipse.core.runtime.IStatus) Component(org.osgi.service.component.annotations.Component) MavenPluginActivator(org.eclipse.m2e.core.internal.MavenPluginActivator) IProject(org.eclipse.core.resources.IProject) MavenProject(org.apache.maven.project.MavenProject) IPath(org.eclipse.core.runtime.IPath) Jar(aQute.bnd.osgi.Jar) IMarker(org.eclipse.core.resources.IMarker) ILogger(org.bndtools.api.ILogger) Analyzer(aQute.bnd.osgi.Analyzer) Job(org.eclipse.core.runtime.jobs.Job) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IPluginExecutionMetadata(org.eclipse.m2e.core.lifecyclemapping.model.IPluginExecutionMetadata) Collection(java.util.Collection) MojoExecution(org.apache.maven.plugin.MojoExecution) BndProjectInfo(org.bndtools.build.api.IProjectDecorator.BndProjectInfo) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) MojoExecutionBuildParticipant(org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant) File(java.io.File) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IJavaModelMarker(org.eclipse.jdt.core.IJavaModelMarker) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) AbstractBuildParticipant2(org.eclipse.m2e.core.project.configurator.AbstractBuildParticipant2) List(java.util.List) IMavenExecutionContext(org.eclipse.m2e.core.embedder.IMavenExecutionContext) Packages(aQute.bnd.osgi.Packages) ICallable(org.eclipse.m2e.core.embedder.ICallable) IResource(org.eclipse.core.resources.IResource) Path(org.eclipse.core.runtime.Path) MavenPlugin(org.eclipse.m2e.core.MavenPlugin) Logger(org.bndtools.api.Logger) IProjectDecorator(org.bndtools.build.api.IProjectDecorator) ProjectConfigurationRequest(org.eclipse.m2e.core.project.configurator.ProjectConfigurationRequest) Reference(org.osgi.service.component.annotations.Reference) IMaven(org.eclipse.m2e.core.embedder.IMaven) IPath(org.eclipse.core.runtime.IPath) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) SubMonitor(org.eclipse.core.runtime.SubMonitor) IProject(org.eclipse.core.resources.IProject) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MavenProject(org.apache.maven.project.MavenProject) IMarker(org.eclipse.core.resources.IMarker) Job(org.eclipse.core.runtime.jobs.Job) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) MojoExecutionBuildParticipant(org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

MojoExecution (org.apache.maven.plugin.MojoExecution)33 MavenProject (org.apache.maven.project.MavenProject)22 MavenSession (org.apache.maven.execution.MavenSession)13 Test (org.junit.Test)10 File (java.io.File)9 ArrayList (java.util.ArrayList)9 List (java.util.List)7 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)7 MavenExecutionPlan (org.apache.maven.lifecycle.MavenExecutionPlan)6 Plugin (org.apache.maven.model.Plugin)6 MojoDescriptor (org.apache.maven.plugin.descriptor.MojoDescriptor)5 SubMonitor (org.eclipse.core.runtime.SubMonitor)5 IMaven (org.eclipse.m2e.core.embedder.IMaven)5 ResolverConfiguration (org.eclipse.m2e.core.project.ResolverConfiguration)5 Path (java.nio.file.Path)4 MavenProjectStub (org.apache.maven.plugin.testing.stubs.MavenProjectStub)4 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 IStatus (org.eclipse.core.runtime.IStatus)4 IMavenExecutionContext (org.eclipse.m2e.core.embedder.IMavenExecutionContext)4 Set (java.util.Set)3