Search in sources :

Example 91 with MavenSession

use of org.apache.maven.execution.MavenSession in project antlr4 by antlr.

the class Antlr4MojoTest method importTokens.

@Test
public void importTokens() throws Exception {
    Path baseDir = resources.getBasedir("importTokens").toPath();
    Path antlrDir = baseDir.resolve("src/main/antlr4");
    Path generatedSources = baseDir.resolve("target/generated-sources/antlr4");
    Path genParser = generatedSources.resolve("test/SimpleParser.java");
    Path tokens = antlrDir.resolve("imports/SimpleLexer.tokens");
    MavenProject project = maven.readMavenProject(baseDir.toFile());
    MavenSession session = maven.newMavenSession(project);
    MojoExecution exec = maven.newMojoExecution("antlr4");
    // //////////////////////////////////////////////////////////////////////
    // 1st - all grammars have to be processed
    // //////////////////////////////////////////////////////////////////////
    assertFalse(Files.exists(genParser));
    maven.executeMojo(session, project, exec);
    assertTrue(Files.exists(genParser));
    // //////////////////////////////////////////////////////////////////////
    // 2nd - nothing has been modified, no grammars have to be processed
    // //////////////////////////////////////////////////////////////////////
    {
        byte[] sum = checksum(genParser);
        maven.executeMojo(session, project, exec);
        assertTrue(Arrays.equals(sum, checksum(genParser)));
    }
    try (Change change = Change.of(tokens, "DOT=4")) {
        byte[] sum = checksum(genParser);
        maven.executeMojo(session, project, exec);
        assertFalse(Arrays.equals(sum, checksum(genParser)));
    }
}
Also used : Path(java.nio.file.Path) MavenSession(org.apache.maven.execution.MavenSession) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) Test(org.junit.Test)

Example 92 with MavenSession

use of org.apache.maven.execution.MavenSession in project antlr4 by antlr.

the class Antlr4MojoTest method importsStandardLayout.

@Test
public void importsStandardLayout() throws Exception {
    Path baseDir = resources.getBasedir("importsStandard").toPath();
    Path antlrDir = baseDir.resolve("src/main/antlr4");
    Path generatedSources = baseDir.resolve("target/generated-sources/antlr4");
    Path genTestLexer = generatedSources.resolve("test/TestLexer.java");
    Path genTestParser = generatedSources.resolve("test/TestParser.java");
    Path genHello = generatedSources.resolve("test/HelloParser.java");
    Path baseGrammar = antlrDir.resolve("imports/TestBaseLexer.g4");
    Path baseGrammar2 = antlrDir.resolve("imports/TestBaseLexer2.g4");
    Path lexerGrammar = antlrDir.resolve("test/TestLexer.g4");
    Path parserGrammar = antlrDir.resolve("test/TestParser.g4");
    MavenProject project = maven.readMavenProject(baseDir.toFile());
    MavenSession session = maven.newMavenSession(project);
    MojoExecution exec = maven.newMojoExecution("antlr4");
    // //////////////////////////////////////////////////////////////////////
    // 1st - all grammars have to be processed
    // //////////////////////////////////////////////////////////////////////
    assertFalse(Files.exists(genHello));
    assertFalse(Files.exists(genTestParser));
    assertFalse(Files.exists(genTestLexer));
    maven.executeMojo(session, project, exec);
    assertTrue(Files.exists(genHello));
    assertTrue(Files.exists(genTestParser));
    assertTrue(Files.exists(genTestLexer));
    byte[] origTestLexerSum = checksum(genTestLexer);
    byte[] origTestParserSum = checksum(genTestParser);
    byte[] origHelloSum = checksum(genHello);
    // //////////////////////////////////////////////////////////////////////
    // 2nd - nothing has been modified, no grammars have to be processed
    // //////////////////////////////////////////////////////////////////////
    {
        maven.executeMojo(session, project, exec);
        assertTrue(Arrays.equals(origTestLexerSum, checksum(genTestLexer)));
        assertTrue(Arrays.equals(origTestParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(origHelloSum, checksum(genHello)));
    }
    // modify the grammar to make checksum comparison detect a change
    try (Change change = Change.of(baseGrammar, "DOT: '.' ;")) {
        maven.executeMojo(session, project, exec);
        assertFalse(Arrays.equals(origTestLexerSum, checksum(genTestLexer)));
        assertFalse(Arrays.equals(origTestParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(origHelloSum, checksum(genHello)));
    }
    // Restore file and confirm it was restored.
    maven.executeMojo(session, project, exec);
    assertTrue(Arrays.equals(origTestLexerSum, checksum(genTestLexer)));
    assertTrue(Arrays.equals(origTestParserSum, checksum(genTestParser)));
    assertTrue(Arrays.equals(origHelloSum, checksum(genHello)));
    // modify the grammar to make checksum comparison detect a change
    try (Change change = Change.of(baseGrammar2, "BANG: '!' ;")) {
        maven.executeMojo(session, project, exec);
        assertFalse(Arrays.equals(origTestLexerSum, checksum(genTestLexer)));
        assertFalse(Arrays.equals(origTestParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(origHelloSum, checksum(genHello)));
    }
    // Restore file and confirm it was restored.
    maven.executeMojo(session, project, exec);
    assertTrue(Arrays.equals(origTestLexerSum, checksum(genTestLexer)));
    assertTrue(Arrays.equals(origTestParserSum, checksum(genTestParser)));
    assertTrue(Arrays.equals(origHelloSum, checksum(genHello)));
    // modify the grammar to make checksum comparison detect a change
    try (Change change = Change.of(lexerGrammar, "FOO: 'foo' ;")) {
        maven.executeMojo(session, project, exec);
        assertFalse(Arrays.equals(origTestLexerSum, checksum(genTestLexer)));
        assertFalse(Arrays.equals(origTestParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(origHelloSum, checksum(genHello)));
    }
    // Restore file and confirm it was restored.
    maven.executeMojo(session, project, exec);
    assertTrue(Arrays.equals(origTestLexerSum, checksum(genTestLexer)));
    assertTrue(Arrays.equals(origTestParserSum, checksum(genTestParser)));
    assertTrue(Arrays.equals(origHelloSum, checksum(genHello)));
    // modify the grammar to make checksum comparison detect a change
    try (Change change = Change.of(parserGrammar, " t : WS* ;")) {
        maven.executeMojo(session, project, exec);
        assertTrue(Arrays.equals(origTestLexerSum, checksum(genTestLexer)));
        assertFalse(Arrays.equals(origTestParserSum, checksum(genTestParser)));
        assertTrue(Arrays.equals(origHelloSum, checksum(genHello)));
    }
    // Restore file and confirm it was restored.
    maven.executeMojo(session, project, exec);
    assertTrue(Arrays.equals(origTestLexerSum, checksum(genTestLexer)));
    assertTrue(Arrays.equals(origTestParserSum, checksum(genTestParser)));
    assertTrue(Arrays.equals(origHelloSum, checksum(genHello)));
}
Also used : Path(java.nio.file.Path) MavenSession(org.apache.maven.execution.MavenSession) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) Test(org.junit.Test)

Example 93 with MavenSession

use of org.apache.maven.execution.MavenSession in project maven-plugins by apache.

the class DefaultDependencyResolverTest method newMavenSession.

protected MavenSession newMavenSession(MavenProject project) {
    MavenExecutionRequest request = new DefaultMavenExecutionRequest();
    MavenExecutionResult result = new DefaultMavenExecutionResult();
    MavenRepositorySystemSession repoSession = new MavenRepositorySystemSession();
    repoSession.setLocalRepositoryManager(LegacyLocalRepositoryManager.wrap(new StubArtifactRepository("target/local-repo"), null));
    MavenSession session = new MavenSession(getContainer(), repoSession, request, result);
    session.setCurrentProject(project);
    session.setProjects(Arrays.asList(project));
    return session;
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) StubArtifactRepository(org.apache.maven.plugin.testing.stubs.StubArtifactRepository) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession)

Example 94 with MavenSession

use of org.apache.maven.execution.MavenSession in project maven-plugins by apache.

the class AssemblyExpressionEvaluatorTest method testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId.

public void testShouldResolveContextValueBeforeModelPropertyOrModelGroupIdInAssemblyId() throws ExpressionEvaluationException {
    final Model model = new Model();
    model.setArtifactId("artifact-id");
    model.setGroupId("group.id");
    model.setVersion("1");
    model.setPackaging("jar");
    final Properties props = new Properties();
    props.setProperty("groupId", "other.id");
    model.setProperties(props);
    final EasyMockSupport mm = new EasyMockSupport();
    MavenSession session = mm.createControl().createMock(MavenSession.class);
    final Properties execProps = new Properties();
    execProps.setProperty("groupId", "still.another.id");
    PropertiesBasedValueSource cliProps = new PropertiesBasedValueSource(execProps);
    expect(session.getExecutionProperties()).andReturn(execProps).anyTimes();
    expect(session.getUserProperties()).andReturn(new Properties()).anyTimes();
    AssemblerConfigurationSource cs = mm.createControl().createMock(AssemblerConfigurationSource.class);
    expect(cs.getCommandLinePropsInterpolator()).andReturn(FixedStringSearchInterpolator.create(cliProps)).anyTimes();
    expect(cs.getRepositoryInterpolator()).andReturn(FixedStringSearchInterpolator.create()).anyTimes();
    expect(cs.getEnvInterpolator()).andReturn(FixedStringSearchInterpolator.create()).anyTimes();
    expect(cs.getMavenSession()).andReturn(session).anyTimes();
    expect(cs.getProject()).andReturn(new MavenProject(model));
    final IMocksControl lrCtl = mm.createControl();
    final ArtifactRepository lr = lrCtl.createMock(ArtifactRepository.class);
    expect(lr.getBasedir()).andReturn("/path/to/local/repo").anyTimes();
    expect(cs.getLocalRepository()).andReturn(lr).anyTimes();
    mm.replayAll();
    final Object result = new AssemblyExpressionEvaluator(cs).evaluate("assembly.${groupId}");
    assertEquals("assembly.still.another.id", result);
    mm.verifyAll();
}
Also used : IMocksControl(org.easymock.classextension.IMocksControl) MavenSession(org.apache.maven.execution.MavenSession) EasyMockSupport(org.easymock.classextension.EasyMockSupport) MavenProject(org.apache.maven.project.MavenProject) Model(org.apache.maven.model.Model) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) AssemblerConfigurationSource(org.apache.maven.plugins.assembly.AssemblerConfigurationSource) Properties(java.util.Properties) PropertiesBasedValueSource(org.codehaus.plexus.interpolation.fixed.PropertiesBasedValueSource)

Example 95 with MavenSession

use of org.apache.maven.execution.MavenSession in project maven-plugins by apache.

the class AssemblyFormatUtilsTest method testEvalFileNameMapping_ShouldResolveArtifactIdAndBaseVersionInOutDir_UseArtifactInfo_WithValidMainProject.

public void testEvalFileNameMapping_ShouldResolveArtifactIdAndBaseVersionInOutDir_UseArtifactInfo_WithValidMainProject() throws AssemblyFormattingException {
    final MavenProject mainProject = createProject("group", "main", "1", null);
    final String artifactVersion = "2-20070807.112233-1";
    final String artifactBaseVersion = "2-SNAPSHOT";
    final MavenProject artifactProject = createProject("group", "artifact", artifactVersion, null);
    final ArtifactMock artifactMock = new ArtifactMock(mockManager, "group", "artifact", artifactVersion, "jar", true, artifactBaseVersion);
    artifactProject.setArtifact(artifactMock.getArtifact());
    final MavenSession session = mockManager.createMock(MavenSession.class);
    expect(session.getExecutionProperties()).andReturn(null).anyTimes();
    expect(session.getUserProperties()).andReturn(new Properties()).anyTimes();
    final AssemblerConfigurationSource cs = mockManager.createMock(AssemblerConfigurationSource.class);
    expect(cs.getMavenSession()).andReturn(session).anyTimes();
    DefaultAssemblyArchiverTest.setupInterpolators(cs);
    mockManager.replayAll();
    final String result = AssemblyFormatUtils.evaluateFileNameMapping("${artifact.artifactId}-${artifact.baseVersion}", artifactMock.getArtifact(), mainProject, null, cs, AssemblyFormatUtils.moduleProjectInterpolator(null), AssemblyFormatUtils.artifactProjectInterpolator(artifactProject));
    /*
        final Artifact artifact = artifactMock.getArtifact();
        final String result =
            AssemblyFormatUtils.evaluateFileNameMapping( "${artifact.artifactId}-${artifact.baseVersion}",
                                                         moduleArtifactInterpolator( null ),
                                                         moduleProjectInterpolator( null ),
                                                         artifactInterpolator( artifact ),
                                                         artifactProjectInterpolator( artifactProject ),
                                                         mainArtifactPropsOnly( mainProject ),
                                                         classifierRules( artifact ),
                                                         FixedStringSearchInterpolator.empty() );

         */
    assertEquals("artifact-2-SNAPSHOT", result);
    mockManager.verifyAll();
    // clear out for next call.
    mockManager.resetAll();
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) MavenProject(org.apache.maven.project.MavenProject) ArtifactMock(org.apache.maven.plugins.assembly.archive.task.testutils.ArtifactMock) AssemblerConfigurationSource(org.apache.maven.plugins.assembly.AssemblerConfigurationSource) Properties(java.util.Properties)

Aggregations

MavenSession (org.apache.maven.execution.MavenSession)118 File (java.io.File)65 MavenProject (org.apache.maven.project.MavenProject)47 SimpleLocalRepositoryManager (org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager)30 DefaultRepositorySystemSession (org.sonatype.aether.util.DefaultRepositorySystemSession)23 Test (org.junit.Test)21 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)20 LegacySupport (org.apache.maven.plugin.LegacySupport)20 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)18 Properties (java.util.Properties)17 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)16 MojoExecution (org.apache.maven.plugin.MojoExecution)13 MavenRepositorySystemSession (org.apache.maven.repository.internal.MavenRepositorySystemSession)13 DefaultMavenExecutionResult (org.apache.maven.execution.DefaultMavenExecutionResult)12 Artifact (org.apache.maven.artifact.Artifact)10 Xpp3Dom (org.codehaus.plexus.util.xml.Xpp3Dom)9 ManipulationSession (org.commonjava.maven.ext.core.ManipulationSession)8 MavenExecutionResult (org.apache.maven.execution.MavenExecutionResult)7 Settings (org.apache.maven.settings.Settings)7 ArrayList (java.util.ArrayList)6