1
2 /*
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 *
19 * This software is copyright (c) 2015 by Alex Heneveld and Cloudsoft Corporation.
20 *
21 * code lifted from https://github.com/ahgittin/license-audit-maven-plugin
22 */
23 package org.apache.rat.mp;
24
25 import java.io.File;
26 import java.util.Arrays;
27
28 import org.apache.maven.DefaultMaven;
29 import org.apache.maven.Maven;
30 import org.apache.maven.execution.DefaultMavenExecutionRequest;
31 import org.apache.maven.execution.DefaultMavenExecutionResult;
32 import org.apache.maven.execution.MavenExecutionRequest;
33 import org.apache.maven.execution.MavenExecutionRequestPopulator;
34 import org.apache.maven.execution.MavenExecutionResult;
35 import org.apache.maven.execution.MavenSession;
36 import org.apache.maven.plugin.Mojo;
37 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.maven.project.ProjectBuilder;
40 import org.apache.maven.project.ProjectBuildingRequest;
41 import org.eclipse.aether.DefaultRepositorySystemSession;
42 import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
43 import org.eclipse.aether.repository.LocalRepository;
44
45 /**
46 * Use this as you would {@link AbstractMojoTestCase}, where you want more of
47 * the standard maven defaults to be set (and where the
48 * {@link AbstractMojoTestCase} leaves them as null or empty). This includes:
49 * <li>local repo, repo sessions and managers configured
50 * <li>maven default remote repos installed (NB: this does not use your ~/.m2
51 * local settings)
52 * <li>system properties are copies
53 * <p>
54 * No changes to subclass code is needed; this simply intercepts the
55 * {@link #newMavenSession(MavenProject)} method used by the various
56 * {@link #lookupMojo(String, File)} methods.
57 * <p>
58 * This also provides new methods, {@link #newMavenSession()} to conveniently
59 * create a maven session, and {@link #lookupConfiguredMojo(File, String)} so
60 * you don't have to always build the project yourself.
61 */
62 public abstract class BetterAbstractMojoTestCase extends AbstractMojoTestCase {
63
64 protected MavenSession newMavenSession() {
65 try {
66 MavenExecutionRequest request = new DefaultMavenExecutionRequest();
67 MavenExecutionResult result = new DefaultMavenExecutionResult();
68
69 // populate sensible defaults, including repository basedir and remote repos
70 MavenExecutionRequestPopulator populator;
71 populator = getContainer().lookup(MavenExecutionRequestPopulator.class);
72 populator.populateDefaults(request);
73
74 // this is needed to allow java profiles to get resolved; i.e. avoid during
75 // project builds:
76 // [ERROR] Failed to determine Java version for profile java-1.5-detected @
77 // org.apache.commons:commons-parent:22,
78 // /Users/alex/.m2/repository/org/apache/commons/commons-parent/22/commons-parent-22.pom,
79 // line 909, column 14
80 request.setSystemProperties(System.getProperties());
81
82 // and this is needed so that the repo session in the maven session
83 // has a repo manager, and it points at the local repo
84 // (cf MavenRepositorySystemUtils.newSession() which is what is otherwise done)
85 DefaultMaven maven = (DefaultMaven) getContainer().lookup(Maven.class);
86 DefaultRepositorySystemSession repoSession = (DefaultRepositorySystemSession) maven
87 .newRepositorySession(request);
88 repoSession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(repoSession,
89 new LocalRepository(request.getLocalRepository().getBasedir())));
90
91 return new MavenSession(getContainer(), repoSession, request, result);
92 } catch (Exception e) {
93 throw new RuntimeException(e);
94 }
95 }
96
97 /**
98 * Extends the super to use the new {@link #newMavenSession()} introduced here
99 * which sets the defaults one expects from maven; the standard test case leaves
100 * a lot of things blank
101 */
102 @Override
103 protected MavenSession newMavenSession(MavenProject project) {
104 MavenSession session = newMavenSession();
105 session.setCurrentProject(project);
106 session.setProjects(Arrays.asList(project));
107 return session;
108 }
109
110 /**
111 * As {@link #lookupConfiguredMojo(MavenProject, String)} but taking the pom
112 * file and creating the {@link MavenProject}.
113 */
114 protected Mojo lookupConfiguredMojo(File pom, String goal) throws Exception {
115 assertNotNull(pom);
116 assertTrue(pom.exists());
117
118 ProjectBuildingRequest buildingRequest = newMavenSession().getProjectBuildingRequest();
119 ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
120 MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();
121
122 return lookupConfiguredMojo(project, goal);
123 }
124
125 }