1 package org.codehaus.mojo.appassembler.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
40
41
42
43 public class DependencyFactory
44 {
45
46
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
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
67 path = file.getPath().replace( '\\', '/' );
68 }
69 catch ( InterpolationException e )
70 {
71
72
73 }
74 }
75 dependency.setRelativePath( path );
76
77 return dependency;
78 }
79
80
81
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
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 }