1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.buildtools.maven2.plugin.wagon;
20
21 import java.io.File;
22
23 import org.apache.maven.artifact.manager.WagonManager;
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.project.MavenProject;
27 import org.apache.maven.wagon.ConnectionException;
28 import org.apache.maven.wagon.ResourceDoesNotExistException;
29 import org.apache.maven.wagon.TransferFailedException;
30 import org.apache.maven.wagon.UnsupportedProtocolException;
31 import org.apache.maven.wagon.Wagon;
32 import org.apache.maven.wagon.authentication.AuthenticationException;
33 import org.apache.maven.wagon.authorization.AuthorizationException;
34 import org.apache.maven.wagon.observers.Debug;
35 import org.apache.maven.wagon.repository.Repository;
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class WagonMojo extends AbstractMojo
50 {
51
52
53
54
55
56
57 private File inputDirectory;
58
59
60
61
62
63
64
65 private String id;
66
67
68
69
70
71
72
73 private String url;
74
75
76
77
78
79 private MavenProject project;
80
81
82
83
84 private WagonManager wagonManager;
85
86 public void execute()
87 throws MojoExecutionException
88 {
89 if ( !inputDirectory.exists() )
90 {
91 throw new MojoExecutionException( "The inputDirectory does not exist" );
92 }
93
94 Repository repository = new Repository( id, url );
95
96
97 Wagon wagon = null;
98 try
99 {
100 wagon = wagonManager.getWagon( repository.getProtocol() );
101 }
102 catch ( UnsupportedProtocolException e )
103 {
104 throw new MojoExecutionException( "Unsupported protocol: '" + repository.getProtocol() + "'", e );
105 }
106
107 if ( !wagon.supportsDirectoryCopy() )
108 {
109 throw new MojoExecutionException(
110 "Wagon protocol '" + repository.getProtocol() + "' doesn't support directory copying" );
111 }
112
113 try
114 {
115 Debug debug = new Debug();
116
117 wagon.addSessionListener( debug );
118
119 wagon.addTransferListener( debug );
120
121 wagon.connect( repository, wagonManager.getAuthenticationInfo( id ) );
122
123 wagon.putDirectory( inputDirectory, "." );
124
125 }
126 catch ( ResourceDoesNotExistException e )
127 {
128 throw new MojoExecutionException( "Error uploading", e );
129 }
130 catch ( TransferFailedException e )
131 {
132 throw new MojoExecutionException( "Error uploading", e );
133 }
134 catch ( AuthorizationException e )
135 {
136 throw new MojoExecutionException( "Error uploading", e );
137 }
138 catch ( ConnectionException e )
139 {
140 throw new MojoExecutionException( "Error uploading", e );
141 }
142 catch ( AuthenticationException e )
143 {
144 throw new MojoExecutionException( "Error uploading", e );
145 }
146 finally
147 {
148 try
149 {
150 wagon.disconnect();
151 }
152 catch ( ConnectionException e )
153 {
154 getLog().error( "Error disconnecting wagon - ignored", e );
155 }
156 }
157 }
158 }