Coverage Report - org.codehaus.mojo.nbm.CreateClusterMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
CreateClusterMojo
0%
0/68
0%
0/22
6.667
CreateClusterMojo$1
0%
0/2
N/A
6.667
CreateClusterMojo$2
0%
0/2
N/A
6.667
 
 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  0
 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  0
         Project antProject = registerNbmAntTasks();
 73  
 
 74  0
         if ( !nbmBuildDir.exists() )
 75  
         {
 76  0
             nbmBuildDir.mkdirs();
 77  
         }
 78  
 
 79  0
         if ( reactorProjects != null && reactorProjects.size() > 0 )
 80  
         {
 81  0
             for ( MavenProject proj : reactorProjects )
 82  
             {
 83  
                 //TODO how to figure where the the buildDir/nbm directory is
 84  0
                 File nbmDir = new File( proj.getBasedir(),
 85  
                         "target" + File.separator + "nbm" + File.separator + "netbeans" );
 86  0
                 if ( nbmDir.exists() )
 87  
                 {
 88  0
                     Copy copyTask = (Copy) antProject.createTask( "copy" );
 89  0
                     copyTask.setTodir( nbmBuildDir );
 90  0
                     copyTask.setOverwrite( true );
 91  0
                     FileSet set = new FileSet();
 92  0
                     set.setDir( nbmDir );
 93  0
                     set.createInclude().setName( "**" );
 94  0
                     copyTask.addFileset( set );
 95  
 
 96  
                     try
 97  
                     {
 98  0
                         copyTask.execute();
 99  
                     }
 100  0
                     catch ( BuildException ex )
 101  
                     {
 102  0
                         getLog().error( "Cannot merge modules into cluster" );
 103  0
                         throw new MojoExecutionException(
 104  
                                 "Cannot merge modules into cluster", ex );
 105  0
                     }
 106  0
                 }
 107  
                 else
 108  
                 {
 109  0
                     if ( "nbm".equals( proj.getPackaging() ) )
 110  
                     {
 111  0
                         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  0
                         throw new MojoFailureException( error );
 117  
                     }
 118  0
                     if ( "bundle".equals( proj.getPackaging() ) )
 119  
                     {
 120  0
                         Artifact art = proj.getArtifact();
 121  0
                         final ExamineManifest mnf = new ExamineManifest( getLog() );
 122  
 
 123  0
                         File jar = new File( proj.getBuild().getDirectory(), proj.getBuild().getFinalName() + ".jar" );
 124  0
                         if ( !jar.exists() )
 125  
                         {
 126  0
                             getLog().error( "Skipping " + proj.getId()
 127  
                                                 + ". Cannot find the main artifact in output directory." );
 128  0
                             continue;
 129  
                         }
 130  0
                         mnf.setJarFile( jar );
 131  0
                         mnf.checkFile();
 132  
 
 133  0
                         File cluster = new File( nbmBuildDir, defaultCluster );
 134  0
                         getLog().debug( "Copying " + art.getId() + " to cluster " + defaultCluster );
 135  0
                         File modules = new File( cluster, "modules" );
 136  0
                         modules.mkdirs();
 137  0
                         File config = new File( cluster, "config" );
 138  0
                         File confModules = new File( config, "Modules" );
 139  0
                         confModules.mkdirs();
 140  0
                         File updateTracting = new File( cluster, "update_tracking" );
 141  0
                         updateTracting.mkdirs();
 142  
 
 143  0
                         final String cnb = mnf.getModule();
 144  0
                         final String cnbDashed = cnb.replace( ".", "-" );
 145  0
                         final File moduleArt = new File( modules, cnbDashed + ".jar" ); //do we need the file in some canotical name pattern?
 146  0
                         final String specVer = mnf.getSpecVersion();
 147  
                         try
 148  
                         {
 149  0
                             FileUtils.copyFile( jar, moduleArt );
 150  0
                             final File moduleConf = new File( confModules, cnbDashed + ".xml" );
 151  0
                             FileUtils.copyStreamToFile( new InputStreamFacade() {
 152  
                                 public InputStream getInputStream() throws IOException
 153  
                                 {
 154  0
                                     return new StringInputStream( CreateClusterAppMojo.createBundleConfigFile( cnb, mnf.isBundleAutoload() ), "UTF-8" );
 155  
                                 }
 156  
                             }, moduleConf );
 157  0
                             FileUtils.copyStreamToFile( new InputStreamFacade() {
 158  
                                 public InputStream getInputStream() throws IOException
 159  
                                 {
 160  0
                                     return new StringInputStream( CreateClusterAppMojo.createBundleUpdateTracking( cnb, moduleArt, moduleConf, specVer ), "UTF-8" );
 161  
                                 }
 162  
                             }, new File( updateTracting, cnbDashed + ".xml" ) );
 163  
                         }
 164  0
                         catch ( IOException exc )
 165  
                         {
 166  0
                             getLog().error( exc );
 167  0
                         }
 168  
 
 169  
                     }
 170  
                 }
 171  0
             }
 172  
             //in 6.1 the rebuilt modules will be cached if the timestamp is not touched.
 173  0
             File[] files = nbmBuildDir.listFiles();
 174  0
             for ( int i = 0; i < files.length; i++ )
 175  
             {
 176  0
                 if ( files[i].isDirectory() )
 177  
                 {
 178  0
                     File stamp = new File( files[i], ".lastModified" );
 179  0
                     if ( !stamp.exists() )
 180  
                     {
 181  
                         try
 182  
                         {
 183  0
                             stamp.createNewFile();
 184  
                         }
 185  0
                         catch ( IOException ex )
 186  
                         {
 187  0
                             ex.printStackTrace();
 188  0
                         }
 189  
                     }
 190  0
                     stamp.setLastModified( new Date().getTime() );
 191  
                 }
 192  
             }
 193  0
             getLog().info( "Created NetBeans module cluster(s) at " + nbmBuildDir );
 194  0
         }
 195  
         else
 196  
         {
 197  0
             throw new MojoExecutionException( "This goal only makes sense on reactor projects." );
 198  
         }
 199  0
     }
 200  
 }