1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40
41
42 public class DumpAuthMojo
43 extends AbstractMojo
44 {
45
46
47
48
49
50
51
52 private File basedir;
53
54
55
56
57
58
59 private WagonManager wagonManager;
60
61
62
63
64
65
66 private File propertiesFile;
67
68
69
70
71
72
73 private String[] serverIds;
74
75
76
77
78
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
149 }
150 }
151 }
152
153 getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + propertiesFile );
154 }
155
156 }