1 package org.apache.maven.shared.release.phase;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.maven.shared.release.ReleaseExecutionException;
27 import org.apache.maven.shared.release.ReleaseResult;
28 import org.apache.maven.shared.release.config.ReleaseDescriptor;
29 import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
30 import org.apache.maven.shared.release.env.ReleaseEnvironment;
31 import org.apache.maven.shared.release.exec.MavenExecutor;
32 import org.apache.maven.shared.release.exec.MavenExecutorException;
33 import org.codehaus.plexus.util.StringUtils;
34
35
36
37
38
39
40 public abstract class AbstractRunGoalsPhase
41 extends AbstractReleasePhase
42 {
43
44
45
46
47
48 private Map<String, MavenExecutor> mavenExecutors;
49
50
51
52
53
54 public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, File workingDirectory,
55 String additionalArguments )
56 throws ReleaseExecutionException
57 {
58 return execute( releaseDescriptor, new DefaultReleaseEnvironment(), workingDirectory, additionalArguments );
59 }
60
61 public ReleaseResult execute( ReleaseDescriptor releaseDescriptor, ReleaseEnvironment releaseEnvironment,
62 File workingDirectory, String additionalArguments )
63 throws ReleaseExecutionException
64 {
65 ReleaseResult result = new ReleaseResult();
66
67 try
68 {
69 String goals = getGoals( releaseDescriptor );
70 if ( !StringUtils.isEmpty( goals ) )
71 {
72 logInfo( result, "Executing goals '" + goals + "'..." );
73
74 MavenExecutor mavenExecutor = mavenExecutors.get( releaseEnvironment.getMavenExecutorId() );
75
76 if ( mavenExecutor == null )
77 {
78 throw new ReleaseExecutionException(
79 "Cannot find Maven executor with id: " + releaseEnvironment.getMavenExecutorId() );
80 }
81
82 File wd =
83 determineWorkingDirectory( workingDirectory,
84 releaseDescriptor.getScmRelativePathProjectDirectory() );
85 mavenExecutor.executeGoals( wd, goals, releaseEnvironment, releaseDescriptor.isInteractive(),
86 additionalArguments, releaseDescriptor.getPomFileName(), result );
87 }
88 }
89 catch ( MavenExecutorException e )
90 {
91 throw new ReleaseExecutionException( e.getMessage(), e );
92 }
93
94 result.setResultCode( ReleaseResult.SUCCESS );
95
96 return result;
97 }
98
99
100
101
102 public void setMavenExecutor( MavenExecutor mavenExecutor )
103 {
104 setMavenExecutor( ReleaseEnvironment.DEFAULT_MAVEN_EXECUTOR_ID, mavenExecutor );
105 }
106
107 public void setMavenExecutor( String id, MavenExecutor executor )
108 {
109 if ( mavenExecutors == null )
110 {
111 mavenExecutors = new HashMap<String, MavenExecutor>();
112 }
113
114 mavenExecutors.put( id, executor );
115 }
116
117 protected abstract String getGoals( ReleaseDescriptor releaseDescriptor );
118
119
120
121
122
123
124
125
126
127
128
129 protected File determineWorkingDirectory( File checkoutDirectory, String relativePathProjectDirectory )
130 {
131 File workingDirectory = checkoutDirectory;
132
133 if ( StringUtils.isNotEmpty( relativePathProjectDirectory ) )
134 {
135 workingDirectory = new File( checkoutDirectory, relativePathProjectDirectory );
136 }
137
138 return workingDirectory;
139 }
140 }