View Javadoc
1   package org.apache.maven.plugin.coreit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.manager.WagonManager;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.wagon.authentication.AuthenticationInfo;
27  
28  import java.io.File;
29  import java.io.FileOutputStream;
30  import java.io.IOException;
31  import java.io.OutputStream;
32  import java.util.Properties;
33  
34  /**
35   * Dumps the authentication info registered with the wagon manager for a server to a properties file.
36   *
37   * @author Benjamin Bentmann
38   *
39   * @goal dump-auth
40   * @phase validate
41   */
42  public class DumpAuthMojo
43      extends AbstractMojo
44  {
45  
46      /**
47       * Project base directory used for manual path alignment.
48       *
49       * @parameter default-value="${basedir}"
50       * @readonly
51       */
52      private File basedir;
53  
54      /**
55       * The Wagon manager used to retrieve authentication infos.
56       *
57       * @component
58       */
59      private WagonManager wagonManager;
60  
61      /**
62       * The path to the properties file used to dump the auth infos.
63       *
64       * @parameter property="wagon.propertiesFile"
65       */
66      private File propertiesFile;
67  
68      /**
69       * The set of server identifiers whose auth infos should be dumped.
70       *
71       * @parameter
72       */
73      private String[] serverIds;
74  
75      /**
76       * Runs this mojo.
77       *
78       * @throws MojoFailureException If the output file could not be created.
79       */
80      public void execute()
81          throws MojoExecutionException, MojoFailureException
82      {
83          Properties authProperties = new Properties();
84  
85          for ( String serverId : serverIds )
86          {
87              getLog().info( "[MAVEN-CORE-IT-LOG] Getting authentication info for server " + serverId );
88  
89              AuthenticationInfo authInfo = wagonManager.getAuthenticationInfo( serverId );
90              if ( authInfo != null )
91              {
92                  if ( authInfo.getUserName() != null )
93                  {
94                      authProperties.setProperty( serverId + ".username", authInfo.getUserName() );
95                  }
96                  if ( authInfo.getPassword() != null )
97                  {
98                      authProperties.setProperty( serverId + ".password", authInfo.getPassword() );
99                  }
100                 if ( authInfo.getPrivateKey() != null )
101                 {
102                     authProperties.setProperty( serverId + ".privateKey", authInfo.getPrivateKey() );
103                 }
104                 if ( authInfo.getPassphrase() != null )
105                 {
106                     authProperties.setProperty( serverId + ".passphrase", authInfo.getPassphrase() );
107                 }
108 
109                 getLog().info( "[MAVEN-CORE-IT-LOG]   username = " + authInfo.getUserName() );
110                 getLog().info( "[MAVEN-CORE-IT-LOG]   password = " + authInfo.getPassword() );
111                 getLog().info( "[MAVEN-CORE-IT-LOG]   private key = " + authInfo.getPrivateKey() );
112                 getLog().info( "[MAVEN-CORE-IT-LOG]   passphrase = " + authInfo.getPassphrase() );
113             }
114             else
115             {
116                 getLog().info( "[MAVEN-CORE-IT-LOG]   (no authentication info available)" );
117             }
118         }
119 
120         if ( !propertiesFile.isAbsolute() )
121         {
122             propertiesFile = new File( basedir, propertiesFile.getPath() );
123         }
124 
125         getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + propertiesFile );
126 
127         OutputStream out = null;
128         try
129         {
130             propertiesFile.getParentFile().mkdirs();
131             out = new FileOutputStream( propertiesFile );
132             authProperties.store( out, "MAVEN-CORE-IT-LOG" );
133         }
134         catch ( IOException e )
135         {
136             throw new MojoExecutionException( "Output file could not be created: " + propertiesFile, e );
137         }
138         finally
139         {
140             if ( out != null )
141             {
142                 try
143                 {
144                     out.close();
145                 }
146                 catch ( IOException e )
147                 {
148                     // just ignore
149                 }
150             }
151         }
152 
153         getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + propertiesFile );
154     }
155 
156 }