View Javadoc

1   /* ==========================================================================
2    * Copyright 2003-2004 Mevenide Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   * =========================================================================
16   */
17  package org.codehaus.mojo.nbm;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.Date;
23  import java.util.List;
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.plugins.annotations.ResolutionScope;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.tools.ant.BuildException;
32  import org.apache.tools.ant.Project;
33  import org.apache.tools.ant.filters.StringInputStream;
34  import org.apache.tools.ant.taskdefs.Copy;
35  import org.apache.tools.ant.types.FileSet;
36  import org.codehaus.mojo.nbm.utils.ExamineManifest;
37  import org.codehaus.plexus.util.FileUtils;
38  import org.codehaus.plexus.util.io.InputStreamFacade;
39  
40  /**
41   * Create the NetBeans module clusters from reactor.
42   * Semi-deprecated; used only for standalone modules and "suites".
43   * @author <a href="mailto:mkleint@codehaus.org">Milos Kleint</a>
44   */
45  @Mojo(name="cluster",aggregator=true, requiresDependencyResolution= ResolutionScope.RUNTIME )
46  public class CreateClusterMojo
47          extends AbstractNbmMojo
48  {
49  
50      /**
51       * directory where the the NetBeans cluster will be created.
52       */
53      @Parameter(defaultValue="${project.build.directory}/netbeans_clusters", required=true)
54      protected File nbmBuildDir;
55  
56      /**
57       * default cluster value for reactor projects without cluster information,
58       * typically OSGi bundles
59       * @since 3.2
60       */
61      @Parameter(defaultValue="extra")
62      private String defaultCluster;
63      /**
64       * If the executed project is a reactor project, this will contains the full list of projects in the reactor.
65       */
66      @Parameter(required=true, readonly=true, property="reactorProjects")
67      private List<MavenProject> reactorProjects;
68  
69      public void execute()
70          throws MojoExecutionException, MojoFailureException
71      {
72          Project antProject = registerNbmAntTasks();
73  
74          if ( !nbmBuildDir.exists() )
75          {
76              nbmBuildDir.mkdirs();
77          }
78  
79          if ( reactorProjects != null && reactorProjects.size() > 0 )
80          {
81              for ( MavenProject proj : reactorProjects )
82              {
83                  //TODO how to figure where the the buildDir/nbm directory is
84                  File nbmDir = new File( proj.getBasedir(),
85                          "target" + File.separator + "nbm" + File.separator + "netbeans" );
86                  if ( nbmDir.exists() )
87                  {
88                      Copy copyTask = (Copy) antProject.createTask( "copy" );
89                      copyTask.setTodir( nbmBuildDir );
90                      copyTask.setOverwrite( true );
91                      FileSet set = new FileSet();
92                      set.setDir( nbmDir );
93                      set.createInclude().setName( "**" );
94                      copyTask.addFileset( set );
95  
96                      try
97                      {
98                          copyTask.execute();
99                      }
100                     catch ( BuildException ex )
101                     {
102                         getLog().error( "Cannot merge modules into cluster" );
103                         throw new MojoExecutionException(
104                                 "Cannot merge modules into cluster", ex );
105                     }
106                 }
107                 else
108                 {
109                     if ( "nbm".equals( proj.getPackaging() ) )
110                     {
111                         String error =
112                             "The NetBeans binary directory structure for "
113                                 + proj.getId()
114                                 + " is not created yet."
115                                 + "\n Please execute 'mvn install nbm:cluster' to build all relevant projects in the reactor.";
116                         throw new MojoFailureException( error );
117                     }
118                     if ( "bundle".equals( proj.getPackaging() ) )
119                     {
120                         Artifact art = proj.getArtifact();
121                         final ExamineManifest mnf = new ExamineManifest( getLog() );
122 
123                         File jar = new File( proj.getBuild().getDirectory(), proj.getBuild().getFinalName() + ".jar" );
124                         if ( !jar.exists() )
125                         {
126                             getLog().error( "Skipping " + proj.getId()
127                                                 + ". Cannot find the main artifact in output directory." );
128                             continue;
129                         }
130                         mnf.setJarFile( jar );
131                         mnf.checkFile();
132 
133                         File cluster = new File( nbmBuildDir, defaultCluster );
134                         getLog().debug( "Copying " + art.getId() + " to cluster " + defaultCluster );
135                         File modules = new File( cluster, "modules" );
136                         modules.mkdirs();
137                         File config = new File( cluster, "config" );
138                         File confModules = new File( config, "Modules" );
139                         confModules.mkdirs();
140                         File updateTracting = new File( cluster, "update_tracking" );
141                         updateTracting.mkdirs();
142 
143                         final String cnb = mnf.getModule();
144                         final String cnbDashed = cnb.replace( ".", "-" );
145                         final File moduleArt = new File( modules, cnbDashed + ".jar" ); //do we need the file in some canotical name pattern?
146                         final String specVer = mnf.getSpecVersion();
147                         try
148                         {
149                             FileUtils.copyFile( jar, moduleArt );
150                             final File moduleConf = new File( confModules, cnbDashed + ".xml" );
151                             FileUtils.copyStreamToFile( new InputStreamFacade() {
152                                 public InputStream getInputStream() throws IOException
153                                 {
154                                     return new StringInputStream( CreateClusterAppMojo.createBundleConfigFile( cnb, mnf.isBundleAutoload() ), "UTF-8" );
155                                 }
156                             }, moduleConf );
157                             FileUtils.copyStreamToFile( new InputStreamFacade() {
158                                 public InputStream getInputStream() throws IOException
159                                 {
160                                     return new StringInputStream( CreateClusterAppMojo.createBundleUpdateTracking( cnb, moduleArt, moduleConf, specVer ), "UTF-8" );
161                                 }
162                             }, new File( updateTracting, cnbDashed + ".xml" ) );
163                         }
164                         catch ( IOException exc )
165                         {
166                             getLog().error( exc );
167                         }
168 
169                     }
170                 }
171             }
172             //in 6.1 the rebuilt modules will be cached if the timestamp is not touched.
173             File[] files = nbmBuildDir.listFiles();
174             for ( int i = 0; i < files.length; i++ )
175             {
176                 if ( files[i].isDirectory() )
177                 {
178                     File stamp = new File( files[i], ".lastModified" );
179                     if ( !stamp.exists() )
180                     {
181                         try
182                         {
183                             stamp.createNewFile();
184                         }
185                         catch ( IOException ex )
186                         {
187                             ex.printStackTrace();
188                         }
189                     }
190                     stamp.setLastModified( new Date().getTime() );
191                 }
192             }
193             getLog().info( "Created NetBeans module cluster(s) at " + nbmBuildDir );
194         }
195         else
196         {
197             throw new MojoExecutionException( "This goal only makes sense on reactor projects." );
198         }
199     }
200 }