Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DumpVersionMojo |
|
| 9.0;9 |
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 | ||
26 | import java.io.File; | |
27 | import java.io.FileOutputStream; | |
28 | import java.io.IOException; | |
29 | import java.io.InputStream; | |
30 | import java.io.OutputStream; | |
31 | import java.util.Properties; | |
32 | ||
33 | /** | |
34 | * Dumps the version info for a wagon provider to a properties file. | |
35 | * | |
36 | * @goal dump-version | |
37 | * @phase validate | |
38 | * | |
39 | * @author Benjamin Bentmann | |
40 | */ | |
41 | 0 | public class DumpVersionMojo |
42 | extends AbstractMojo | |
43 | { | |
44 | ||
45 | /** | |
46 | * Project base directory used for manual path alignment. | |
47 | * | |
48 | * @parameter default-value="${basedir}" | |
49 | * @readonly | |
50 | */ | |
51 | private File basedir; | |
52 | ||
53 | /** | |
54 | * The Wagon manager used to lookup the wagon of interest. | |
55 | * | |
56 | * @component | |
57 | */ | |
58 | private WagonManager wagonManager; | |
59 | ||
60 | /** | |
61 | * The path to the properties file used to dump the auth infos. | |
62 | * | |
63 | * @parameter expression="${wagon.propertiesFile}" | |
64 | */ | |
65 | private File propertiesFile; | |
66 | ||
67 | /** | |
68 | * The role hint for the provider of interest. | |
69 | * | |
70 | * @parameter expression="${wagon.providerHint}" | |
71 | */ | |
72 | private String providerHint; | |
73 | ||
74 | /** | |
75 | * The group id for the provider of interest. | |
76 | * | |
77 | * @parameter expression="${wagon.providerGroupId}" | |
78 | */ | |
79 | private String providerGroupId; | |
80 | ||
81 | /** | |
82 | * The artifact id for the provider of interest. | |
83 | * | |
84 | * @parameter expression="${wagon.providerArtifactId}" | |
85 | */ | |
86 | private String providerArtifactId; | |
87 | ||
88 | /** | |
89 | * Runs this mojo. | |
90 | * | |
91 | * @throws MojoExecutionException If the output file could not be created. | |
92 | */ | |
93 | public void execute() | |
94 | throws MojoExecutionException | |
95 | { | |
96 | 0 | Properties wagonProperties = new Properties(); |
97 | ||
98 | Object wagon; | |
99 | try | |
100 | { | |
101 | 0 | wagon = wagonManager.getWagon( providerHint ); |
102 | ||
103 | 0 | String resource = "/META-INF/maven/" + providerGroupId + "/" + providerArtifactId + "/pom.properties"; |
104 | 0 | InputStream is = wagon.getClass().getResourceAsStream( resource ); |
105 | 0 | wagonProperties.load( is ); |
106 | } | |
107 | 0 | catch ( IOException e ) |
108 | { | |
109 | 0 | throw new MojoExecutionException( "Wagon properties could not be read: " + e.getMessage(), e ); |
110 | } | |
111 | 0 | catch ( Exception e ) |
112 | { | |
113 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] No wagon available for " + providerHint ); |
114 | 0 | wagonProperties.setProperty( "missing", "true" ); |
115 | 0 | } |
116 | ||
117 | 0 | if ( !propertiesFile.isAbsolute() ) |
118 | { | |
119 | 0 | propertiesFile = new File( basedir, propertiesFile.getPath() ); |
120 | } | |
121 | ||
122 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + propertiesFile ); |
123 | ||
124 | 0 | OutputStream out = null; |
125 | try | |
126 | { | |
127 | 0 | propertiesFile.getParentFile().mkdirs(); |
128 | 0 | out = new FileOutputStream( propertiesFile ); |
129 | 0 | wagonProperties.store( out, "MAVEN-CORE-IT-LOG" ); |
130 | } | |
131 | 0 | catch ( IOException e ) |
132 | { | |
133 | 0 | throw new MojoExecutionException( "Output file could not be created: " + propertiesFile, e ); |
134 | } | |
135 | finally | |
136 | { | |
137 | 0 | if ( out != null ) |
138 | { | |
139 | try | |
140 | { | |
141 | 0 | out.close(); |
142 | } | |
143 | 0 | catch ( IOException e ) |
144 | { | |
145 | // just ignore | |
146 | 0 | } |
147 | } | |
148 | } | |
149 | ||
150 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + propertiesFile ); |
151 | 0 | } |
152 | ||
153 | } |