View Javadoc

1   /**
2    * Copyright (C) 2010-2012 Joerg Bellmann <joerg.bellmann@googlemail.com>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.googlecode.t7mp;
17  
18  import java.util.Collection;
19  import java.util.List;
20  
21  import org.apache.maven.model.Dependency;
22  
23  import com.google.common.base.Predicate;
24  import com.google.common.collect.Collections2;
25  import com.google.common.collect.Lists;
26  import com.googlecode.t7mp.steps.Context;
27  import com.googlecode.t7mp.steps.Step;
28  
29  public class CheckT7ArtifactsStep implements Step {
30  
31      private Predicate<AbstractArtifact> noVersionPredicate;
32  
33      @Deprecated
34      private AbstractT7BaseMojo mojo;
35      private T7Configuration configuration;
36      private PluginLog log;
37      private final Collection<AbstractArtifact> noVersionArtifacts = Lists.newArrayList();
38  
39      @SuppressWarnings("unchecked")
40      @Override
41      public void execute(Context context) {
42          mojo = ((MavenPluginContext) context).getMojo();
43          configuration = context.getConfiguration();
44          log = context.getLog();
45          noVersionPredicate = new NoVersionPredicate(log);
46          log.debug("Fitler libs");
47          noVersionArtifacts.addAll(Collections2.filter(configuration.getWebapps(), noVersionPredicate));
48          log.debug("Filter webapps");
49          noVersionArtifacts.addAll(Collections2.filter(configuration.getLibs(), noVersionPredicate));
50          if (noVersionArtifacts.size() > 0) {
51              log.debug("artifacts without version found ");
52              List<Dependency> projectDependencies = mojo.getMavenProject().getDependencies();
53              List<Dependency> managedDependencies = mojo.getMavenProject().getDependencyManagement().getDependencies();
54              if (log.isDebugEnabled()) {
55                  log.debug("Project-Dependencies : " + projectDependencies.size());
56                  logDependencies(projectDependencies);
57                  log.debug("Managed-Dependencies : " + managedDependencies.size());
58                  logDependencies(managedDependencies);
59              }
60              Predicate<Dependency> depsfilter = new FindVersionPredicate(noVersionArtifacts, log);
61              log.debug("Filter projectArtifacts");
62              // first managed dependencies
63              Collection<Dependency> depsApplied = Collections2.filter(managedDependencies, depsfilter);
64              // project dependencies can overwrite filtering results
65              depsApplied.addAll(Collections2.filter(projectDependencies, depsfilter));
66              log.debug(depsApplied.size() + " dependenciey applied");
67              log.debug("check for noversion-artifacts again ...");
68              Collection<AbstractArtifact> noVersionsFound = Collections2.filter(noVersionArtifacts, noVersionPredicate);
69              if (noVersionsFound.size() > 0) {
70                  for (AbstractArtifact artifact : noVersionsFound) {
71                      log.error("No version configured for artifact --" + artifact.toString());
72                  }
73                  throw new TomcatSetupException("ConfigurationException");
74              }
75          }
76      }
77  
78      protected void logDependencies(List<Dependency> dependencies) {
79          for (Dependency dependency : dependencies) {
80              log.debug("found dependency : " + dependency.toString() + " groupId:" + dependency.getGroupId() + " artifactId:" + dependency.getArtifactId()
81                      + " version:" + dependency.getVersion() + " packaging:" + dependency.getType());
82          }
83      }
84  
85  }