View Javadoc

1   /*
2    * DaCapo Benchmark Maven Plugin
3    * Copyright (C) 2010 Technische Universität Darmstadt
4    * info@scalabench.org
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.scalabench.plugins.dacapo;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.util.List;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.codehaus.plexus.archiver.ArchiverException;
27  import org.codehaus.plexus.archiver.FileSet;
28  import org.codehaus.plexus.archiver.util.DefaultFileSet;
29  import org.codehaus.plexus.archiver.zip.ZipArchiver;
30  
31  /**
32   * Package the benchmark's input data.
33   * 
34   * @since 0.1.0
35   * 
36   * @threadSafe
37   * @goal package-data
38   * @phase prepare-package
39   */
40  public class PackageDataMojo extends AbstractDacapoBenchmarkMojo {
41      
42      /**
43       * Main directory for input data to include as a ZIP file in the JAR.
44       * 
45       * @required
46       * @parameter default-value="${basedir}/src/main/data"
47       */
48      private File dataSourceDirectory;
49      
50      /**
51       * Additional directories for input data to include.
52       * 
53       * @parameter
54       */
55      private List<File> additionalDataSourceDirectories;
56      
57      /**
58       * The data output directory.
59       * 
60       * @required
61       * @readonly
62       * @parameter expression="${project.build.outputDirectory}/dat"
63       */
64      private File dataOutputDirectory;
65      
66      /**
67       * Should the input data be compressed?
68       * 
69       * @required
70       * @parameter default-value=true
71       */
72      private boolean compressData;
73      
74      /**
75       * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="zip"
76       */
77      private ZipArchiver zipArchiver;
78      
79      @Override
80      public void execute() throws MojoExecutionException, MojoFailureException {
81          final File zipDataFile = new File(dataOutputDirectory, getBenchmarkName() + ".zip");
82          
83          try {
84              addDirectory(dataSourceDirectory);
85              
86              if (additionalDataSourceDirectories != null)
87                  for (File additionalDataSourceDirectory : additionalDataSourceDirectories)
88                      addDirectory(additionalDataSourceDirectory);
89              
90              createDataPackage(zipDataFile);
91          } catch (ArchiverException e) {
92              throw new MojoExecutionException("Could not package input data", e);
93          } catch (IOException e) {
94              throw new MojoExecutionException("Could not package input data", e);
95          }
96      }
97      
98      private void createDataPackage(final File zipDataFile) throws ArchiverException, IOException {
99          if (!zipArchiver.getResources().hasNext()) {
100             logWarn("No input data to package");
101         } else {
102             zipArchiver.setDestFile(zipDataFile);
103             zipArchiver.setCompress(compressData);
104             zipArchiver.createArchive();
105         }
106     }
107     
108     private void addDirectory(File directory) throws ArchiverException {
109         if (!directory.exists()) {
110             logInfo("Skipping non-existing data source directory %s", directory);
111         } else {
112             logInfo("Packaging data source directory %s", directory);
113             final FileSet fs = createDirectoryFileSet(directory);
114             zipArchiver.addFileSet(fs);
115         }
116     }
117     
118     private FileSet createDirectoryFileSet(File directory) {
119         final DefaultFileSet fs = new DefaultFileSet();
120         fs.setDirectory(directory);
121         fs.setPrefix(getBenchmarkName() + File.separatorChar);
122         return fs;
123     }
124 }