public void execute()

in nbm-maven-plugin/src/main/java/org/apache/netbeans/nbm/NetBeansManifestUpdateMojo.java [303:624]


    public void execute()
            throws MojoExecutionException, MojoFailureException

    {
        //need to do this to chekc for javahelp on CP.
        super.registerNbmAntTasks();
        NetBeansModule module;
        if ( descriptor != null && descriptor.exists() )
        {
            module = readModuleDescriptor( descriptor );
            getLog().warn( "descriptor parameter is deprecated, use equivalent mojo parameters instead." );
        }
        else
        {
            module = createDefaultDescriptor( project, false );
        }

        String mtype = moduleType;
        //same moduleType related code in CreateNetBeansFileStructure.java
        if ( "normal".equals( mtype ) && module.getModuleType() != null )
        {
            mtype = module.getModuleType();
            getLog().warn( "moduleType in module descriptor is deprecated, use the plugin's parameter moduleType" );
        }
        if ( !"normal".equals( mtype ) && !"autoload".equals( mtype ) && !"eager".equals( mtype ) && !"disabled".
                equals( mtype ) )
        {
            getLog().error( "Only 'normal,autoload,eager,disabled' are allowed values in the moduleType parameter" );
        }
        boolean autoload = "autoload".equals( mtype );
        boolean eager = "eager".equals( mtype );

        String moduleName = codeNameBase;
        if ( module.getCodeNameBase() != null )
        {
            moduleName = module.getCodeNameBase();
            getLog().warn( "codeNameBase in module descriptor is deprecated, use the plugin's parameter codeNameBase" );
        }
        moduleName = moduleName.replaceAll( "-", "." );
//<!-- if a NetBeans specific manifest is defined, examine this one, otherwise the already included one.
// ignoring the case when some of the NetBeans attributes are already defined in the jar and more is included.
        File specialManifest = sourceManifestFile;
        File nbmManifest = ( module.getManifest() != null ? new File(
                project.getBasedir(), module.getManifest() ) : null );
        if ( nbmManifest != null && nbmManifest.exists() )
        {
            //deprecated, but if actually defined, will use it.
            specialManifest = nbmManifest;
        }
        ExamineManifest examinator = new ExamineManifest( getLog() );
        if ( specialManifest != null && specialManifest.exists() )
        {
            examinator.setManifestFile( specialManifest );
            examinator.checkFile();
        }
        else
        {
//            examinator.setJarFile( jarFile );
        }

        getLog().info( "NBM Plugin generates manifest" );

        Manifest manifest = null;
        if ( specialManifest != null && specialManifest.exists() )
        {
            Reader reader = null;
            try
            {
                reader = new InputStreamReader( new FileInputStream( specialManifest ) );
                manifest = new Manifest( reader );
            }
            catch ( IOException exc )
            {
                manifest = new Manifest();
                getLog().warn( "Error reading manifest at " + specialManifest, exc );
            }
            catch ( ManifestException ex )
            {
                getLog().warn( "Error reading manifest at " + specialManifest, ex );
                manifest = new Manifest();
            }
            finally
            {
                IOUtil.close( reader );
            }
        }
        else
        {
            manifest = new Manifest();
        }
        Date date = new Date();
        String specVersion = AdaptNbVersion.adaptVersion( project.getVersion(),
                AdaptNbVersion.TYPE_SPECIFICATION, date );
        String implVersion = AdaptNbVersion.adaptVersion( project.getVersion(),
                AdaptNbVersion.TYPE_IMPLEMENTATION, date );
        Manifest.Section mainSection = manifest.getMainSection();
        conditionallyAddAttribute( mainSection,
                "OpenIDE-Module-Specification-Version", specVersion );
        conditionallyAddAttribute( mainSection,
                "OpenIDE-Module-Implementation-Version", implVersion );
        if ( autoload || eager )
        { //MNBMODULE-194
            conditionallyAddAttribute( mainSection, "AutoUpdate-Show-In-Client", "false" );
        }
        final String timestamp = createTimestamp( date );
        conditionallyAddAttribute( mainSection, "OpenIDE-Module-Build-Version",
                timestamp );
        String projectCNB = conditionallyAddAttribute( mainSection, "OpenIDE-Module", moduleName );
        String packagesValue;
        if ( publicPackages != null && publicPackages.size() > 0 )
        {
            StringBuilder sb = new StringBuilder();
            for ( String pub : publicPackages )
            {
                if ( pub == null )
                { //#MNBMODULE-237
                    continue;
                }
                if ( pub.endsWith( ".**" ) )
                {
                    // well, just sort of wrong value but accept
                    sb.append( pub );
                }
                else if ( pub.endsWith( ".*" ) )
                {
                    //multipackage value
                    sb.append( pub ).append( "*" );
                }
                else
                {
                    sb.append( pub ).append( ".*" );
                }
                sb.append( ", " );
            }
            if ( sb.length() > 1 )
            { //if only item is null, we have empty builder
                sb.setLength( sb.length() - 2 ); //cut the last 2 ", " characters
                packagesValue = sb.toString();
            }
            else
            {
                // no packages available
                packagesValue = "-";
            }
        }
        else
        {
            // no packages available
            packagesValue = "-";
        }
        conditionallyAddAttribute( mainSection, "OpenIDE-Module-Public-Packages", packagesValue );

        //See http://www.netbeans.org/download/dev/javadoc/org-openide-modules/apichanges.html#split-of-openide-jar
        conditionallyAddAttribute( mainSection, "OpenIDE-Module-Requires",
                "org.openide.modules.ModuleFormat1" );
//        conditionallyAddAttribute(mainSection, "OpenIDE-Module-IDE-Dependencies", "IDE/1 > 3.40");
        // localization items
        if ( !examinator.isLocalized() )
        {
            conditionallyAddAttribute( mainSection,
                    "OpenIDE-Module-Display-Category", project.getGroupId() );
            conditionallyAddAttribute( mainSection, "OpenIDE-Module-Name",
                    project.getName() );
            conditionallyAddAttribute( mainSection,
                    "OpenIDE-Module-Short-Description", shorten( project.getDescription() ) );
            conditionallyAddAttribute( mainSection,
                    "OpenIDE-Module-Long-Description", project.getDescription() );
        }
        getLog().debug( "module =" + module );

        final String scope = includeRuntimeModuleLibraries ? Artifact.SCOPE_COMPILE_PLUS_RUNTIME : Artifact.SCOPE_COMPILE;
        ProjectBuildingRequest prjbr = new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
        prjbr.setProject( project );
        DependencyNode treeroot = createDependencyTree( prjbr, dependencyGraphBuilder, scope );
        Map<Artifact, ExamineManifest> examinerCache = new HashMap<Artifact, ExamineManifest>();
        @SuppressWarnings( "unchecked" )
        List<Artifact> libArtifacts = getLibraryArtifacts( treeroot, module, project.getRuntimeArtifacts(),
                examinerCache, getLog(), useOSGiDependencies );
        List<ModuleWrapper> moduleArtifacts = getModuleDependencyArtifacts( treeroot, module, moduleDependencies,
                project, examinerCache,
                libArtifacts, getLog(), useOSGiDependencies );
        StringBuilder classPath = new StringBuilder();
        StringBuilder mavenClassPath = new StringBuilder();
        String dependencies = "";
        String depSeparator = " ";

        for ( Artifact a : libArtifacts )
        {
            if ( classPath.length() > 0 )
            {
                classPath.append( ' ' );
            }
            classPath.append( artifactToClassPathEntry( a, codeNameBase ) );
            if ( mavenClassPath.length() > 0 )
            {
                mavenClassPath.append( ' ' );
            }
            mavenClassPath.append( a.getGroupId() ).
                    append( ':' ).
                    append( a.getArtifactId() ).
                    append( ':' ).
                    append( a.getBaseVersion() );

            if ( a.getClassifier() != null )
            {
                mavenClassPath.append( ":" ).append( a.getClassifier() );
            }
        }

        for ( ModuleWrapper wr : moduleArtifacts )
        {
            if ( wr.transitive )
            {
                continue;
            }
            Dependency dep = wr.dependency;
            Artifact artifact = wr.artifact;
            ExamineManifest depExaminator = examinerCache.get( artifact );
            String type = dep.getType();
            String depToken = dep.getExplicitValue();
            if ( depToken == null )
            {
                if ( "loose".equals( type ) )
                {
                    depToken = depExaminator.getModuleWithRelease();
                }
                else if ( "spec".equals( type ) )
                {
                    depToken
                            = depExaminator.getModuleWithRelease()
                            + " > "
                            + ( depExaminator.isNetBeansModule() ? depExaminator.getSpecVersion()
                            : AdaptNbVersion.adaptVersion( depExaminator.getSpecVersion(),
                                    AdaptNbVersion.TYPE_SPECIFICATION, date ) );
                }
                else if ( "impl".equals( type ) )
                {
                    depToken
                            = depExaminator.getModuleWithRelease()
                            + " = "
                            + ( depExaminator.isNetBeansModule() ? depExaminator.getImplVersion()
                            : AdaptNbVersion.adaptVersion( depExaminator.getImplVersion(),
                                    AdaptNbVersion.TYPE_IMPLEMENTATION, date ) );
                }
                else
                {
                    throw new MojoExecutionException(
                            "Wrong type of NetBeans dependency: " + type + " Allowed values are: loose, spec, impl." );
                }
            }
            if ( depToken == null )
            {
                //TODO report
                getLog().error(
                        "Cannot properly resolve the NetBeans dependency for " + dep.getId() );
            }
            else
            {
                dependencies = dependencies + depSeparator + depToken;
                depSeparator = ", ";
            }
        }
        if ( !verifyRuntime.equalsIgnoreCase( SKIP ) )
        {
            try
            {
                checkModuleClassPath( treeroot, libArtifacts, examinerCache, moduleArtifacts, projectCNB );
            }
            catch ( IOException ex )
            {
                throw new MojoExecutionException( "Error while checking runtime dependencies", ex );
            }
        }

        if ( nbmJavahelpSource.exists() )
        {
            String moduleJarName = stripVersionFromCodebaseName( moduleName ).replace( ".", "-" );
            classPath.append( " docs/" ).append( moduleJarName ).append( ".jar" );
        }

        if ( classPath.length() > 0 )
        {
            conditionallyAddAttribute( mainSection, "X-Class-Path", classPath.toString().trim() );
        }
        if ( mavenClassPath.length() > 0 )
        {
            conditionallyAddAttribute( mainSection, "Maven-Class-Path", mavenClassPath.toString() );
        }
        if ( dependencies.length() > 0 )
        {
            conditionallyAddAttribute( mainSection, "OpenIDE-Module-Module-Dependencies", dependencies );
        }
//            if ( librList.size() > 0 )
//            {
//                String list = "";
//                for ( int i = 0; i < librList.size(); i++ )
//                {
//                    list = list + " " + librList.get( i );
//                }
//                getLog().warn(
//                        "Some libraries could not be found in the dependency chain: " + list );
//            }
        PrintWriter writer = null;
        try
        {
            if ( !targetManifestFile.exists() )
            {
                targetManifestFile.getParentFile().mkdirs();
                targetManifestFile.createNewFile();
            }
            writer = new PrintWriter( targetManifestFile, "UTF-8" ); //TODO really UTF-8??
            manifest.write( writer );
        }
        catch ( IOException ex )
        {
            throw new MojoExecutionException( ex.getMessage(), ex );
        }
        finally
        {
            IOUtil.close( writer );
        }
    }