1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package org.apache.maven.plugin.eclipse.writers; |
15 | |
|
16 | |
import java.io.File; |
17 | |
import java.io.IOException; |
18 | |
|
19 | |
import org.apache.maven.plugin.MojoExecutionException; |
20 | |
import org.apache.maven.plugin.eclipse.Constants; |
21 | |
import org.apache.maven.plugin.eclipse.EclipseSourceDir; |
22 | |
import org.apache.maven.plugin.eclipse.Messages; |
23 | |
import org.apache.maven.plugin.ide.IdeUtils; |
24 | |
import org.apache.maven.plugin.ide.JeeUtils; |
25 | |
import org.apache.maven.plugin.logging.Log; |
26 | |
import org.apache.maven.project.MavenProject; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class EclipseManifestWriter |
37 | |
extends AbstractEclipseManifestWriter |
38 | |
{ |
39 | |
|
40 | 0 | private static final String GENERATED_RESOURCE_DIRNAME = |
41 | |
"target" + File.separatorChar + "generated-resources" + File.separatorChar + "eclipse"; |
42 | |
|
43 | 0 | private static final String WEBAPP_RESOURCE_DIR = |
44 | |
"src" + File.separatorChar + "main" + File.separatorChar + "webapp"; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
private static String getWebContentBaseDirectory( EclipseWriterConfig config ) |
55 | |
throws MojoExecutionException |
56 | |
{ |
57 | |
|
58 | 0 | File warSourceDirectory = |
59 | |
new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN, |
60 | |
"warSourceDirectory", WEBAPP_RESOURCE_DIR ) ); |
61 | |
|
62 | 0 | String webContentDir = |
63 | |
IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false ); |
64 | |
|
65 | |
|
66 | 0 | String result = config.getProject().getBasedir().getAbsolutePath() + File.separatorChar + webContentDir; |
67 | |
|
68 | 0 | return result; |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
protected String getMetaInfBaseDirectory( MavenProject project ) |
78 | |
throws MojoExecutionException |
79 | |
{ |
80 | 0 | String metaInfBaseDirectory = null; |
81 | |
|
82 | 0 | if ( this.config.getProject().getPackaging().equals( Constants.PROJECT_PACKAGING_WAR ) ) |
83 | |
{ |
84 | |
|
85 | |
|
86 | 0 | metaInfBaseDirectory = getWebContentBaseDirectory( this.config ); |
87 | |
|
88 | 0 | this.log.debug( "Attempting to use: " + metaInfBaseDirectory + " for location of META-INF in war project." ); |
89 | |
|
90 | 0 | File metaInfDirectoryFile = |
91 | |
new File( metaInfBaseDirectory + File.separatorChar + AbstractEclipseManifestWriter.META_INF_DIRECTORY ); |
92 | |
|
93 | 0 | if ( !metaInfDirectoryFile.exists() |
94 | |
|| ( metaInfDirectoryFile.exists() && !metaInfDirectoryFile.isDirectory() ) ) |
95 | |
{ |
96 | 0 | metaInfBaseDirectory = null; |
97 | |
} |
98 | |
} |
99 | |
|
100 | 0 | for ( int index = this.config.getSourceDirs().length - 1; metaInfBaseDirectory == null && index >= 0; index-- ) |
101 | |
{ |
102 | |
|
103 | 0 | File manifestFile = |
104 | |
new File( this.config.getEclipseProjectDirectory(), this.config.getSourceDirs()[index].getPath() |
105 | |
+ File.separatorChar + AbstractEclipseManifestWriter.META_INF_DIRECTORY + File.separatorChar |
106 | |
+ AbstractEclipseManifestWriter.MANIFEST_MF_FILENAME ); |
107 | |
|
108 | 0 | this.log.debug( "Checking for existence of META-INF/MANIFEST.MF file: " + manifestFile ); |
109 | |
|
110 | 0 | if ( manifestFile.exists() ) |
111 | |
{ |
112 | 0 | metaInfBaseDirectory = manifestFile.getParentFile().getParent(); |
113 | |
} |
114 | |
} |
115 | |
|
116 | 0 | return metaInfBaseDirectory; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public static void addManifestResource( Log log, EclipseWriterConfig config ) |
126 | |
throws MojoExecutionException |
127 | |
{ |
128 | |
|
129 | 0 | AbstractEclipseManifestWriter manifestWriter = new EclipseManifestWriter(); |
130 | 0 | manifestWriter.init( log, config ); |
131 | |
|
132 | 0 | String packaging = config.getProject().getPackaging(); |
133 | |
|
134 | 0 | String manifestDirectory = manifestWriter.getMetaInfBaseDirectory( config.getProject() ); |
135 | |
|
136 | 0 | if ( !Constants.PROJECT_PACKAGING_EAR.equals( packaging ) |
137 | |
&& !Constants.PROJECT_PACKAGING_WAR.equals( packaging ) && manifestDirectory == null ) |
138 | |
{ |
139 | |
|
140 | 0 | String generatedResourceDir = |
141 | |
config.getProject().getBasedir().getAbsolutePath() + File.separatorChar |
142 | |
+ EclipseManifestWriter.GENERATED_RESOURCE_DIRNAME; |
143 | |
|
144 | 0 | manifestDirectory = generatedResourceDir + File.separatorChar + "META-INF"; |
145 | |
|
146 | |
try |
147 | |
{ |
148 | 0 | new File( manifestDirectory ).mkdirs(); |
149 | 0 | File manifestFile = new File( manifestDirectory + File.separatorChar + "MANIFEST.MF" ); |
150 | 0 | if ( manifestFile.exists() ) |
151 | |
{ |
152 | 0 | manifestFile.delete(); |
153 | |
} |
154 | 0 | manifestFile.createNewFile(); |
155 | |
} |
156 | 0 | catch ( IOException e ) |
157 | |
{ |
158 | 0 | log.error( Messages.getString( "EclipsePlugin.cantwritetofile", new Object[] { manifestDirectory |
159 | |
+ File.separatorChar + "META-INF" + File.separatorChar + "MANIFEST.MF" } ) ); |
160 | 0 | } |
161 | |
|
162 | 0 | log.debug( "Adding " + EclipseManifestWriter.GENERATED_RESOURCE_DIRNAME + " to eclipse sources " ); |
163 | |
|
164 | 0 | EclipseSourceDir[] sourceDirs = config.getSourceDirs(); |
165 | 0 | EclipseSourceDir[] newSourceDirs = new EclipseSourceDir[sourceDirs.length + 1]; |
166 | 0 | System.arraycopy( sourceDirs, 0, newSourceDirs, 0, sourceDirs.length ); |
167 | 0 | newSourceDirs[sourceDirs.length] = |
168 | |
new EclipseSourceDir( EclipseManifestWriter.GENERATED_RESOURCE_DIRNAME, null, true, false, null, null, |
169 | |
false ); |
170 | 0 | config.setSourceDirs( newSourceDirs ); |
171 | |
} |
172 | |
|
173 | 0 | if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) ) |
174 | |
{ |
175 | 0 | new File( getWebContentBaseDirectory( config ) + File.separatorChar + "META-INF" ).mkdirs(); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | 0 | manifestWriter.write(); |
182 | 0 | } |
183 | |
} |