View Javadoc
1   /*
2    * #%L
3    * Mojo's Maven plugin for Cobertura
4    * %%
5    * Copyright (C) 2005 - 2013 Codehaus
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  /*
21   * Copyright 2011
22   *
23   * Licensed under the Apache License, Version 2.0 (the "License");
24   * you may not use this file except in compliance with the License.
25   * You may obtain a copy of the License at
26   *
27   *  http://www.apache.org/licenses/LICENSE-2.0
28   *
29   * Unless required by applicable law or agreed to in writing, software
30   * distributed under the License is distributed on an "AS IS" BASIS,
31   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32   * See the License for the specific language governing permissions and
33   * limitations under the License.
34   */
35  package org.codehaus.mojo.cobertura.tasks;
36  
37  import org.apache.maven.plugin.MojoExecutionException;
38  import org.codehaus.plexus.util.StringUtils;
39  
40  import java.io.File;
41  import java.util.Collections;
42  import java.util.List;
43  
44  /**
45   * The Report Task.
46   *
47   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
48   */
49  public class ReportTask
50      extends AbstractTask
51  {
52      private File dataFile;
53  
54      private File outputDirectory;
55  
56      private String outputFormat;
57  
58      private String sourceEncoding;
59  
60      private List<String> compileSourceRoots;
61  
62      /**
63       * Create ReportTask.
64       */
65      public ReportTask()
66      {
67          super( net.sourceforge.cobertura.reporting.ReportMain.class.getName() );
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      public void execute()
74          throws MojoExecutionException
75      {
76          outputDirectory.mkdirs();
77  
78          for ( String directory : compileSourceRoots )
79          {
80              cmdLineArgs.addArg( "--source", directory );
81          }
82  
83          if ( outputDirectory != null )
84          {
85              cmdLineArgs.addArg( "--destination", outputDirectory.getAbsolutePath() );
86          }
87  
88          if ( dataFile != null )
89          {
90              cmdLineArgs.addArg( "--datafile", dataFile.getAbsolutePath() );
91          }
92  
93          if ( StringUtils.isNotEmpty( outputFormat ) )
94          {
95              cmdLineArgs.addArg( "--format", outputFormat );
96          }
97  
98          if ( StringUtils.isNotEmpty( sourceEncoding ) )
99          {
100             cmdLineArgs.addArg( "--encoding", sourceEncoding );
101         }
102 
103         int returnCode = executeJava();
104 
105         // Check the return code and print a message
106         if ( returnCode == 0 )
107         {
108             getLog().info( "Cobertura Report generation was successful." );
109         }
110         else
111         {
112             throw new MojoExecutionException( "Unable to generate Cobertura Report for project." );
113         }
114     }
115 
116     /**
117      * @return Returns the dataFile.
118      */
119     public File getDataFile()
120     {
121         return dataFile;
122     }
123 
124     /**
125      * @return Returns the outputDirectory.
126      */
127     public File getOutputDirectory()
128     {
129         return outputDirectory;
130     }
131 
132     /**
133      * @return Returns the outputFormat.
134      */
135     public String getOutputFormat()
136     {
137         return outputFormat;
138     }
139 
140     /**
141      * @return Returns the sourceEncoding.
142      */
143     public String getSourceEncoding()
144     {
145         return sourceEncoding;
146     }
147 
148     /**
149      * @param dataFile The dataFile to set.
150      */
151     public void setDataFile( File dataFile )
152     {
153         this.dataFile = dataFile;
154     }
155 
156     /**
157      * @param outputDirectory The outputDirectory to set.
158      */
159     public void setOutputDirectory( File outputDirectory )
160     {
161         this.outputDirectory = outputDirectory;
162     }
163 
164     /**
165      * @param outputFormat The outputFormat to set.
166      */
167     public void setOutputFormat( String outputFormat )
168     {
169         this.outputFormat = outputFormat;
170     }
171 
172     /**
173      * @param sourceEncoding The sourceEncoding to set.
174      */
175     public void setSourceEncoding( String sourceEncoding )
176     {
177         this.sourceEncoding = sourceEncoding;
178     }
179 
180     /**
181      * Set the list of compile source roots.
182      *
183      * @param compileSourceRoots the source roots.
184      */
185     public void setCompileSourceRoots( List<String> compileSourceRoots )
186     {
187         this.compileSourceRoots = Collections.unmodifiableList( compileSourceRoots );
188     }
189 }