View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.gpg.it;
20  
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.io.PrintStream;
24  import java.net.URISyntaxException;
25  import java.net.URL;
26  import java.util.Arrays;
27  import java.util.Properties;
28  
29  import org.apache.commons.io.input.NullInputStream;
30  import org.apache.maven.shared.invoker.DefaultInvocationRequest;
31  import org.apache.maven.shared.invoker.DefaultInvoker;
32  import org.apache.maven.shared.invoker.InvocationOutputHandler;
33  import org.apache.maven.shared.invoker.InvocationRequest;
34  import org.apache.maven.shared.invoker.InvocationResult;
35  import org.apache.maven.shared.invoker.Invoker;
36  import org.apache.maven.shared.invoker.InvokerLogger;
37  import org.apache.maven.shared.invoker.MavenInvocationException;
38  import org.apache.maven.shared.invoker.PrintStreamHandler;
39  import org.apache.maven.shared.invoker.PrintStreamLogger;
40  
41  public class InvokerTestUtils {
42  
43      public static InvocationRequest createRequest(
44              File pomFile, File mavenUserSettings, File gpgHome, String signer, boolean providePassphraseEnv) {
45          final InvocationRequest request = new DefaultInvocationRequest();
46          request.setUserSettingsFile(mavenUserSettings);
47          request.setShowVersion(true);
48          request.setDebug(true);
49          request.setShowErrors(true);
50          request.setTimeoutInSeconds(60); // safeguard against GPG freezes
51          request.setGoals(Arrays.asList("clean", "install"));
52          request.setPomFile(pomFile);
53  
54          if (providePassphraseEnv) {
55              request.addShellEnvironment("MAVEN_GPG_PASSPHRASE", "TEST");
56          }
57  
58          final Properties properties = new Properties();
59          request.setProperties(properties);
60  
61          // Required for JRE 7 to connect to Maven Central with TLSv1.2
62          final String httpsProtocols = System.getProperty("https.protocols");
63          if (httpsProtocols != null && !httpsProtocols.isEmpty()) {
64              properties.setProperty("https.protocols", httpsProtocols);
65          }
66  
67          if (signer != null) {
68              properties.setProperty("gpg.signer", signer);
69              properties.setProperty("gpg.keyFilePath", new File("src/test/resources/signing-key.asc").getAbsolutePath());
70          }
71          properties.setProperty("gpg.homedir", gpgHome.getAbsolutePath());
72  
73          return request;
74      }
75  
76      public static BuildResult executeRequest(
77              final InvocationRequest request, final File mavenHome, final File localRepository)
78              throws FileNotFoundException, MavenInvocationException {
79          final InvocationResult result;
80  
81          final File buildLog =
82                  new File(request.getBaseDirectory(request.getPomFile().getParentFile()), "build.log");
83          try (final PrintStream buildLogStream = new PrintStream(buildLog)) {
84              final InvocationOutputHandler buildLogOutputHandler = new PrintStreamHandler(buildLogStream, false);
85              final InvokerLogger logger = new PrintStreamLogger(buildLogStream, InvokerLogger.DEBUG);
86  
87              final Invoker invoker = new DefaultInvoker();
88              invoker.setMavenHome(mavenHome);
89              invoker.setLocalRepositoryDirectory(localRepository);
90              invoker.setLogger(logger);
91  
92              request.setInputStream(new NullInputStream(0));
93              request.setOutputHandler(buildLogOutputHandler);
94              request.setErrorHandler(buildLogOutputHandler);
95  
96              result = invoker.execute(request);
97          }
98  
99          return new BuildResult(buildLog, result);
100     }
101 
102     public static File getTestResource(final String path) throws URISyntaxException, FileNotFoundException {
103         final URL resourceUrl = InvokerTestUtils.class.getResource(path);
104         if (resourceUrl == null) {
105             throw new FileNotFoundException("Cannot find file " + path);
106         }
107 
108         return new File(resourceUrl.toURI());
109     }
110 }