View Javadoc
1   package org.codehaus.mojo.appassembler.util;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2006-2012, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.File;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.shared.mapping.MappingUtils;
33  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorException;
34  import org.codehaus.mojo.appassembler.model.Dependency;
35  import org.codehaus.plexus.interpolation.InterpolationException;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  /**
39   * A factory that creates Dependency objects.
40   *
41   * @author Dennis Lundberg
42   */
43  public class DependencyFactory
44  {
45      /**
46       * Used by GenericDaemonGenerator.
47       */
48      public static Dependency create( Artifact artifact, ArtifactRepositoryLayout layout, String outputFileNameMapping )
49      {
50          Dependency dependency = new Dependency();
51          dependency.setGroupId( artifact.getGroupId() );
52          dependency.setArtifactId( artifact.getArtifactId() );
53          dependency.setVersion( artifact.getVersion() );
54          dependency.setClassifier( artifact.getClassifier() );
55  
56          String path = layout.pathOf( artifact );
57          if ( StringUtils.isNotEmpty( outputFileNameMapping ) )
58          {
59              // Replace the file name part of the path with one that has been mapped
60              File directory = new File( path ).getParentFile();
61  
62              try
63              {
64                  String fileName = MappingUtils.evaluateFileNameMapping( outputFileNameMapping, artifact );
65                  File file = new File( directory, fileName );
66                  // Always use forward slash as path separator, because that's what layout.pathOf( artifact ) uses
67                  path = file.getPath().replace( '\\', '/' );
68              }
69              catch ( InterpolationException e )
70              {
71                  // TODO Handle exceptions!
72                  // throw new MojoExecutionException("Unable to map file name.", e);
73              }
74          }
75          dependency.setRelativePath( path );
76  
77          return dependency;
78      }
79  
80      /**
81       * Used by AssembleMojo and JavaServiceWrapperDaemonGenerator.
82       */
83      public static Dependency create( Artifact artifact, ArtifactRepositoryLayout layout,
84                                       boolean useTimestampInSnapshotFileName, String outputFileNameMapping )
85      {
86          Dependency dependency = create( artifact, layout, outputFileNameMapping );
87  
88          if ( artifact.isSnapshot() && !useTimestampInSnapshotFileName )
89          {
90              dependency.setRelativePath( ArtifactUtils.pathBaseVersionOf( layout, artifact ) );
91          }
92  
93          return dependency;
94      }
95  
96      /**
97       * Used by AbstractBooterDaemonGenerator.
98       */
99      public static Dependency create( MavenProject project, String id, ArtifactRepositoryLayout layout,
100                                      String outputFileNameMapping )
101         throws DaemonGeneratorException
102     {
103         Artifact artifact = (Artifact) project.getArtifactMap().get( id );
104 
105         if ( artifact == null )
106         {
107             throw new DaemonGeneratorException( "The project has to have a dependency on '" + id + "'." );
108         }
109 
110         return create( artifact, layout, outputFileNameMapping );
111     }
112 }