Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CliConfigMojo |
|
| 2.5;2,5 |
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 java.io.File; | |
23 | import java.util.List; | |
24 | import java.util.Map; | |
25 | import java.util.Properties; | |
26 | import java.util.Set; | |
27 | ||
28 | import org.apache.maven.plugin.AbstractMojo; | |
29 | import org.apache.maven.plugin.MojoExecutionException; | |
30 | ||
31 | /** | |
32 | * Dumps this mojo's configuration into a properties file. | |
33 | * | |
34 | * @goal cli-config | |
35 | * @phase validate | |
36 | * | |
37 | * @author Benjamin Bentmann | |
38 | * @version $Id: CliConfigMojo.java 1070118 2011-02-12 18:11:07Z bentmann $ | |
39 | */ | |
40 | 0 | public class CliConfigMojo |
41 | extends AbstractMojo | |
42 | { | |
43 | ||
44 | /** | |
45 | * The current project's base directory, used for path alignment. | |
46 | * | |
47 | * @parameter default-value="${basedir}" | |
48 | * @readonly | |
49 | */ | |
50 | private File basedir; | |
51 | ||
52 | /** | |
53 | * The path to the properties file into which to save the mojo configuration. | |
54 | * | |
55 | * @parameter expression="${config.propertiesFile}" | |
56 | */ | |
57 | private File propertiesFile; | |
58 | ||
59 | /** | |
60 | * A simple parameter of type {@link java.lang.String}. | |
61 | * | |
62 | * @parameter expression="${config.stringParam}" | |
63 | */ | |
64 | private String stringParam; | |
65 | ||
66 | /** | |
67 | * A simple parameter of type {@link java.io.File}. | |
68 | * | |
69 | * @parameter expression="${config.fileParam}" | |
70 | */ | |
71 | private File fileParam; | |
72 | ||
73 | /** | |
74 | * An array parameter of component type {@link java.lang.String}. | |
75 | * | |
76 | * @parameter expression="${config.stringParams}" | |
77 | */ | |
78 | private String[] stringParams; | |
79 | ||
80 | /** | |
81 | * An array parameter of component type {@link java.io.File}. | |
82 | * | |
83 | * @parameter expression="${config.fileParams}" | |
84 | */ | |
85 | private File[] fileParams; | |
86 | ||
87 | /** | |
88 | * A collection parameter of type {@link java.util.List}. | |
89 | * | |
90 | * @parameter expression="${config.listParam}" | |
91 | */ | |
92 | private List listParam; | |
93 | ||
94 | /** | |
95 | * A collection parameter of type {@link java.util.Set}. | |
96 | * | |
97 | * @parameter expression="${config.setParam}" | |
98 | */ | |
99 | private Set setParam; | |
100 | ||
101 | /** | |
102 | * Runs this mojo. | |
103 | * | |
104 | * @throws MojoExecutionException If the output file could not be created. | |
105 | */ | |
106 | public void execute() | |
107 | throws MojoExecutionException | |
108 | { | |
109 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + propertiesFile ); |
110 | ||
111 | 0 | if ( propertiesFile == null ) |
112 | { | |
113 | 0 | throw new MojoExecutionException( "Path name for output file has not been specified" ); |
114 | } | |
115 | ||
116 | 0 | if ( !propertiesFile.isAbsolute() ) |
117 | { | |
118 | 0 | propertiesFile = new File( basedir, propertiesFile.getPath() ).getAbsoluteFile(); |
119 | } | |
120 | ||
121 | 0 | Properties configProps = new Properties(); |
122 | ||
123 | 0 | dumpConfiguration( configProps ); |
124 | ||
125 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + propertiesFile ); |
126 | ||
127 | 0 | PropertiesUtil.write( propertiesFile, configProps ); |
128 | ||
129 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + propertiesFile ); |
130 | 0 | } |
131 | ||
132 | /** | |
133 | * Dumps the mojo configuration into the specified properties. | |
134 | * | |
135 | * @param props The properties to dump the configuration into, must not be <code>null</code>. | |
136 | */ | |
137 | private void dumpConfiguration( Properties props ) | |
138 | { | |
139 | /* | |
140 | * NOTE: This intentionally does not dump the absolute path of a file to check the actual value that was | |
141 | * injected by Maven. | |
142 | */ | |
143 | 0 | PropertiesUtil.serialize( props, "propertiesFile", propertiesFile ); |
144 | 0 | PropertiesUtil.serialize( props, "stringParam", stringParam ); |
145 | 0 | PropertiesUtil.serialize( props, "fileParam", fileParam ); |
146 | 0 | PropertiesUtil.serialize( props, "stringParams", stringParams ); |
147 | 0 | PropertiesUtil.serialize( props, "fileParams", fileParams ); |
148 | 0 | PropertiesUtil.serialize( props, "listParam", listParam ); |
149 | 0 | PropertiesUtil.serialize( props, "setParam", setParam ); |
150 | 0 | } |
151 | ||
152 | } |