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 public class EclipseManifestWriter
37 extends AbstractEclipseManifestWriter
38 {
39
40 private static final String GENERATED_RESOURCE_DIRNAME =
41 "target" + File.separatorChar + "generated-resources" + File.separatorChar + "eclipse";
42
43 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 File warSourceDirectory =
59 new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
60 "warSourceDirectory", WEBAPP_RESOURCE_DIR ) );
61
62 String webContentDir =
63 IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false );
64
65
66 String result = config.getProject().getBasedir().getAbsolutePath() + File.separatorChar + webContentDir;
67
68 return result;
69 }
70
71
72
73
74
75
76
77 protected String getMetaInfBaseDirectory( MavenProject project )
78 throws MojoExecutionException
79 {
80 String metaInfBaseDirectory = null;
81
82 if ( this.config.getProject().getPackaging().equals( Constants.PROJECT_PACKAGING_WAR ) )
83 {
84
85
86 metaInfBaseDirectory = getWebContentBaseDirectory( this.config );
87
88 this.log.debug( "Attempting to use: " + metaInfBaseDirectory + " for location of META-INF in war project." );
89
90 File metaInfDirectoryFile =
91 new File( metaInfBaseDirectory + File.separatorChar + AbstractEclipseManifestWriter.META_INF_DIRECTORY );
92
93 if ( !metaInfDirectoryFile.exists()
94 || ( metaInfDirectoryFile.exists() && !metaInfDirectoryFile.isDirectory() ) )
95 {
96 metaInfBaseDirectory = null;
97 }
98 }
99
100 for ( int index = this.config.getSourceDirs().length - 1; metaInfBaseDirectory == null && index >= 0; index-- )
101 {
102
103 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 this.log.debug( "Checking for existence of META-INF/MANIFEST.MF file: " + manifestFile );
109
110 if ( manifestFile.exists() )
111 {
112 metaInfBaseDirectory = manifestFile.getParentFile().getParent();
113 }
114 }
115
116 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 AbstractEclipseManifestWriter manifestWriter = new EclipseManifestWriter();
130 manifestWriter.init( log, config );
131
132 String packaging = config.getProject().getPackaging();
133
134 String manifestDirectory = manifestWriter.getMetaInfBaseDirectory( config.getProject() );
135
136 if ( !Constants.PROJECT_PACKAGING_EAR.equals( packaging )
137 && !Constants.PROJECT_PACKAGING_WAR.equals( packaging ) && manifestDirectory == null )
138 {
139
140 String generatedResourceDir =
141 config.getProject().getBasedir().getAbsolutePath() + File.separatorChar
142 + EclipseManifestWriter.GENERATED_RESOURCE_DIRNAME;
143
144 manifestDirectory = generatedResourceDir + File.separatorChar + "META-INF";
145
146 try
147 {
148 new File( manifestDirectory ).mkdirs();
149 File manifestFile = new File( manifestDirectory + File.separatorChar + "MANIFEST.MF" );
150 if ( manifestFile.exists() )
151 {
152 manifestFile.delete();
153 }
154 manifestFile.createNewFile();
155 }
156 catch ( IOException e )
157 {
158 log.error( Messages.getString( "EclipsePlugin.cantwritetofile", new Object[] { manifestDirectory
159 + File.separatorChar + "META-INF" + File.separatorChar + "MANIFEST.MF" } ) );
160 }
161
162 log.debug( "Adding " + EclipseManifestWriter.GENERATED_RESOURCE_DIRNAME + " to eclipse sources " );
163
164 EclipseSourceDir[] sourceDirs = config.getSourceDirs();
165 EclipseSourceDir[] newSourceDirs = new EclipseSourceDir[sourceDirs.length + 1];
166 System.arraycopy( sourceDirs, 0, newSourceDirs, 0, sourceDirs.length );
167 newSourceDirs[sourceDirs.length] =
168 new EclipseSourceDir( EclipseManifestWriter.GENERATED_RESOURCE_DIRNAME, null, true, false, null, null,
169 false );
170 config.setSourceDirs( newSourceDirs );
171 }
172
173 if ( Constants.PROJECT_PACKAGING_WAR.equals( packaging ) )
174 {
175 new File( getWebContentBaseDirectory( config ) + File.separatorChar + "META-INF" ).mkdirs();
176 }
177
178
179
180
181 manifestWriter.write();
182 }
183 }