aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/maven/apache-maven-3.3.3/maven-core/src/test/java/org/apache/maven/MavenLifecycleParticipantTest.java
blob: 17aeab31388180a1b729d7c0f6db5ecf5d12cfaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package org.apache.maven;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.ComponentDescriptor;

public class MavenLifecycleParticipantTest
    extends AbstractCoreMavenComponentTestCase
{

    private static final String INJECTED_ARTIFACT_ID = "injected";

    public static class InjectDependencyLifecycleListener
        extends AbstractMavenLifecycleParticipant
    {

        @Override
        public void afterProjectsRead( MavenSession session )
        {
            MavenProject project = session.getProjects().get( 0 );

            Dependency dependency = new Dependency();
            dependency.setArtifactId( INJECTED_ARTIFACT_ID );
            dependency.setGroupId( "foo" );
            dependency.setVersion( "1.2.3" );
            dependency.setScope( "system" );
            try
            {
                dependency.setSystemPath( new File(
                                                    "src/test/projects/lifecycle-executor/project-with-additional-lifecycle-elements/pom.xml" ).getCanonicalPath() );
            }
            catch ( IOException e )
            {
                throw new RuntimeException( e );
            }

            project.getModel().addDependency( dependency );
        }

        @Override
        public void afterSessionStart( MavenSession session )
        {
            session.getUserProperties().setProperty( "injected", "bar" );
        }

    }

    public static class InjectReactorDependency
        extends AbstractMavenLifecycleParticipant
    {
        @Override
        public void afterProjectsRead( MavenSession session )
        {
            injectReactorDependency( session.getProjects(), "module-a", "module-b" );
        }

        private void injectReactorDependency( List<MavenProject> projects, String moduleFrom, String moduleTo )
        {
            for ( MavenProject project : projects )
            {
                if ( moduleFrom.equals( project.getArtifactId() ) )
                {
                    Dependency dependency = new Dependency();
                    dependency.setArtifactId( moduleTo );
                    dependency.setGroupId( project.getGroupId() );
                    dependency.setVersion( project.getVersion() );

                    project.getModel().addDependency( dependency );
                }
            }
        }
    }

    @Override
    protected void setupContainer()
    {
        super.setupContainer();
    }

    @Override
    protected String getProjectsDirectory()
    {
        return "src/test/projects/lifecycle-listener";
    }

    public void testDependencyInjection()
        throws Exception
    {
        PlexusContainer container = getContainer();

        ComponentDescriptor<? extends AbstractMavenLifecycleParticipant> cd =
            new ComponentDescriptor<InjectDependencyLifecycleListener>( InjectDependencyLifecycleListener.class,
                                                                        container.getContainerRealm() );
        cd.setRoleClass( AbstractMavenLifecycleParticipant.class );
        container.addComponentDescriptor( cd );

        Maven maven = container.lookup( Maven.class );
        File pom = getProject( "lifecycle-listener-dependency-injection" );
        MavenExecutionRequest request = createMavenExecutionRequest( pom );
        request.setGoals( Arrays.asList( "validate" ) );
        MavenExecutionResult result = maven.execute( request );

        assertFalse( result.getExceptions().toString(), result.hasExceptions() );

        MavenProject project = result.getProject();

        assertEquals( "bar", project.getProperties().getProperty( "foo" ) );

        ArrayList<Artifact> artifacts = new ArrayList<Artifact>( project.getArtifacts() );

        assertEquals( 1, artifacts.size() );
        assertEquals( INJECTED_ARTIFACT_ID, artifacts.get( 0 ).getArtifactId() );
    }

    public void testReactorDependencyInjection()
        throws Exception
    {
        List<String> reactorOrder =
            getReactorOrder( "lifecycle-participant-reactor-dependency-injection", InjectReactorDependency.class );
        assertEquals( Arrays.asList( "parent", "module-b", "module-a" ), reactorOrder );
    }

    private <T> List<String> getReactorOrder( String testProject, Class<T> participant )
        throws Exception
    {
        PlexusContainer container = getContainer();

        ComponentDescriptor<T> cd = new ComponentDescriptor<T>( participant, container.getContainerRealm() );
        cd.setRoleClass( AbstractMavenLifecycleParticipant.class );
        container.addComponentDescriptor( cd );

        Maven maven = container.lookup( Maven.class );
        File pom = getProject( testProject );
        MavenExecutionRequest request = createMavenExecutionRequest( pom );
        request.setGoals( Arrays.asList( "validate" ) );
        MavenExecutionResult result = maven.execute( request );

        assertFalse( result.getExceptions().toString(), result.hasExceptions() );

        List<String> order = new ArrayList<String>();
        for ( MavenProject project : result.getTopologicallySortedProjects() )
        {
            order.add( project.getArtifactId() );
        }
        return order;
    }
}