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.util.Set;
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
27  import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
28  import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
29  
30  public abstract class AbstractDacapoBenchmarkMojo extends AbstractMojo {
31      
32      /**
33       * The Maven project being built.
34       * 
35       * @required
36       * @readonly
37       * @parameter expression="${project}"
38       */
39      private MavenProject project;
40      
41      /**
42       * The name of the benchmark. If unspecified, it is derived from the project's <code>artifactId</code>.
43       * 
44       * If the <code>artifactId</code> matches one of the following patterns, the benchmark name is extracted from it as
45       * indicated:
46       * 
47       * <ul>
48       *   <li><code>${benchmarkName}</code>-dacapo-benchmark</li>
49       *   <li>dacapo-<code>${benchmarkName}</code>-benchmark</li>
50       * </ul>
51       * 
52       * Otherwise, the <code>artifactId</code> is taken wholesale.
53       * 
54       * @parameter
55       */
56      private String benchmarkName;
57      
58      public MavenProject getProject() {
59          return project;
60      }
61      
62      public String getBenchmarkName() {
63          if (benchmarkName == null)
64              return getBenchmarkNameFromArtifactId(getProject().getArtifactId());
65          else
66              return benchmarkName;
67      }
68      
69      public static String getBenchmarkNameFromArtifactId(String artifactId) {
70          final String DACAPO_BENCHMARK_SUFFIX = "-dacapo-benchmark";
71          final String DACAPO_PREFIX = "dacapo-";
72          final String BENCHMARK_SUFFIX = "-benchmark";
73          
74          if (artifactId.endsWith(DACAPO_BENCHMARK_SUFFIX))
75              return artifactId.substring(0, artifactId.length() - DACAPO_BENCHMARK_SUFFIX.length());
76          else if (artifactId.startsWith(DACAPO_PREFIX) && artifactId.endsWith(BENCHMARK_SUFFIX))
77              return artifactId.substring(DACAPO_PREFIX.length(), artifactId.length() - BENCHMARK_SUFFIX.length());
78          else
79              return artifactId;
80      }
81      
82      /**
83       * Gets the library artifacts the benchmark depends on.
84       * 
85       * All provided-scoped dependencies are assumed to be dependencies of the benchmark itself (as opposed to dependencies of
86       * the harness).
87       */
88      public Set<Artifact> getLibraries() throws MojoExecutionException {
89          final FilterArtifacts filter = new FilterArtifacts();
90          filter.addFilter(new ScopeFilter(Artifact.SCOPE_PROVIDED, ""));
91          
92          try {
93              return filter.filter(getProject().getArtifacts());
94          } catch (ArtifactFilterException e) {
95              throw new MojoExecutionException(e.getMessage(), e);
96          }
97      }
98      
99      public void logDebug(String format, Object... args) {
100         getLog().debug(String.format(format, args));
101     }
102     
103     public void logInfo(String format, Object... args) {
104         getLog().info(String.format(format, args));
105     }
106     
107     public void logWarn(String format, Object... args) {
108         getLog().warn(String.format(format, args));
109     }
110     
111     public void logError(String format, Object... args) {
112         getLog().error(String.format(format, args));
113     }
114 }