brett 2004/04/20 17:33:22 Modified: src/java/org/apache/maven Tag: MAVEN-1_0-BRANCH MavenSession.java src/java/org/apache/maven/cli Tag: MAVEN-1_0-BRANCH App.java src/java/org/apache/maven/plugin Tag: MAVEN-1_0-BRANCH GoalToJellyScriptHousingMapper.java PluginManager.java Log: complete maven help enhancements Revision Changes Path No revision No revision 1.18.4.8 +12 -1 maven/src/java/org/apache/maven/MavenSession.java Index: MavenSession.java =================================================================== RCS file: /home/cvs/maven/src/java/org/apache/maven/MavenSession.java,v retrieving revision 1.18.4.7 retrieving revision 1.18.4.8 diff -u -r1.18.4.7 -r1.18.4.8 --- MavenSession.java 24 Mar 2004 05:54:08 -0000 1.18.4.7 +++ MavenSession.java 21 Apr 2004 00:33:22 -0000 1.18.4.8 @@ -264,4 +264,15 @@ { pluginManager.attainGoals( project, goals ); } + + public Project getPluginProjectFromGoal( String goal ) throws MavenException + { + return pluginManager.getPluginProjectFromGoal( goal ); + } + + public Set getProjectGoals( Project project ) + { + return pluginManager.getGoalNames( project ); + } } + No revision No revision 1.37.4.20 +156 -158 maven/src/java/org/apache/maven/cli/App.java Index: App.java =================================================================== RCS file: /home/cvs/maven/src/java/org/apache/maven/cli/App.java,v retrieving revision 1.37.4.19 retrieving revision 1.37.4.20 diff -u -r1.37.4.19 -r1.37.4.20 --- App.java 16 Apr 2004 07:00:34 -0000 1.37.4.19 +++ App.java 21 Apr 2004 00:33:22 -0000 1.37.4.20 @@ -28,11 +28,13 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.maven.MavenConstants; +import org.apache.maven.MavenException; import org.apache.maven.MavenSession; import org.apache.maven.MavenUtils; import org.apache.maven.NoGoalException; import org.apache.maven.UnknownGoalException; import org.apache.maven.jelly.MavenJellyContext; +import org.apache.maven.project.Project; import org.apache.maven.verifier.ChecksumVerificationException; import org.apache.maven.verifier.RepoConfigException; import org.apache.maven.verifier.UnsatisfiedDependencyException; @@ -48,8 +50,10 @@ import java.util.Collections; import java.util.Comparator; import java.util.Date; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.StringTokenizer; @@ -65,7 +69,7 @@ * of the goals. The goal computation logic needs to be moved * out of this class and be placed in MavenSession.java proper. * @todo All logging needs to be done via commons-logging. No - * System.out.* and Jelly needs to be taught to take a + * System.err.* and Jelly needs to be taught to take a * logger. In an attempt to isolate everything in MavenSession.java. */ public class App @@ -80,7 +84,10 @@ /** Default file name for an XML-based POM. */ public static final String POM_FILE_NAME = "project.xml"; - /** Default console width - for formatting output - 78 */ + /** Default wrap indent. */ + private static final int WRAP_INDENT = 35; + + /** Default console width - for formatting output. */ private static final int CONSOLE_WIDTH = 80; /** return code for ok processing */ @@ -177,12 +184,6 @@ /** CLI Parser */ private CommandLine commandLine; - /** Saved original console System.out. */ - private PrintStream consoleOut; - - /** Saved original console System.err. */ - private PrintStream consoleErr; - /** Jelly's underlying writer. */ private Writer writer; @@ -193,7 +194,7 @@ private MavenSession mavenSession; /** logger for output */ - private Log log = LogFactory.getLog(App.class); + private Log log = LogFactory.getLog( App.class ); /** Constructor. */ public App() @@ -324,10 +325,7 @@ protected void initializeRootContext() throws IOException { - this.consoleOut = System.out; - this.consoleErr = System.err; - - this.writer = new OutputStreamWriter( this.consoleOut ); + this.writer = new OutputStreamWriter( System.out ); XMLOutput output = XMLOutput.createXMLOutput( writer, false ); if ( getCli().hasOption( WORKING_DIR ) ) @@ -345,7 +343,7 @@ // We will assume here that there might not be a project.xml file present // and we will create the root context from the directory gleaned from // the user.dir system property. - MavenJellyContext c = MavenUtils.createContext( new File( System.getProperty("user.dir") ) ); + MavenJellyContext c = MavenUtils.createContext( new File( System.getProperty( "user.dir" ) ) ); setRootContext( c ); getRootContext().setXMLOutput( output ); @@ -527,7 +525,7 @@ System.err.println( "Reference made to goal '" + e.getGoal().getName() + "' which has no definition." ); - System.out.println( "" ); + System.err.println( "" ); returnCode = RC_EMPTY_GOAL; return; } @@ -569,7 +567,7 @@ if ( !failed ) { - System.out.println( "BUILD SUCCESSFUL" ); + log.info( "BUILD SUCCESSFUL" ); } final long mb = 1024 * 1024; @@ -643,7 +641,7 @@ /** * Display the plugin help if the option is present, then exit */ - private void displayPluginHelp() + private void displayPluginHelp() throws MavenException { if ( getCli().hasOption( DISPLAY_PLUGIN_HELP ) ) { @@ -651,7 +649,14 @@ displayGoals( true, plugin ); - // TODO: show project information for plugin + if ( plugin != null ) + { + Project project = mavenSession.getPluginProjectFromGoal( plugin ); + if ( project != null && project.getDescription() != null ) + { + log.info( wrapConsoleMessage( project.getDescription(), 0, CONSOLE_WIDTH ) ); + } + } exit( RC_OK ); return; @@ -665,9 +670,16 @@ { if ( getCli().hasOption( DISPLAY_USAGE ) ) { - // TODO: show maven.xml goals - // TODO: show maven.xml goal hooks - // TODO: show project information + Set goals = mavenSession.getProjectGoals( mavenSession.getRootProject() ); + + String title = "Project Goals"; + log.info( title ); + log.info( format( "", title.length(), '=' ) ); + + displayGoals( false, null, goals ); + + log.info( wrapConsoleMessage( mavenSession.getRootProject().getDescription(), 0, CONSOLE_WIDTH ) ); + exit( RC_OK ); return; } @@ -695,7 +707,6 @@ if ( getCli().hasOption( DISPLAY_VERSION ) ) { printConsoleMavenHeader(); - System.out.println( "Java version: " + System.getProperty( "java.version" ) ); exit( RC_OK ); return; } @@ -711,7 +722,7 @@ { int returnCode = RC_GOAL_FAILED; - System.out.println( "" ); + System.err.println( "" ); System.err.println( "BUILD FAILED" ); String msg = null; @@ -823,15 +834,15 @@ } /** - * Prints the MavenSession header to System.out + * Prints the MavenSession header. */ protected void printConsoleMavenHeader() { - System.out.println( " __ __" ); - System.out.println( "| \\/ |__ _Apache__ ___" ); - System.out.println( "| |\\/| / _` \\ V / -_) ' \\ ~ intelligent projects ~" ); - System.out.println( "|_| |_\\__,_|\\_/\\___|_||_| v. " + org.apache.maven.MavenSession.APP_VERSION ); - System.out.println( "" ); + log.info( " __ __" ); + log.info( "| \\/ |__ _Apache__ ___" ); + log.info( "| |\\/| / _` \\ V / -_) ' \\ ~ intelligent projects ~" ); + log.info( "|_| |_\\__,_|\\_/\\___|_||_| v. " + org.apache.maven.MavenSession.APP_VERSION ); + log.info( "" ); } @@ -858,158 +869,89 @@ } log.info( title ); log.info( format( "", title.length(), '=' ) ); - log.info( "" ); Set goals = mavenSession.getAllGoalNames(); + displayGoals( pluginOnly, plugin, goals ); + } - List list = new ArrayList( goals ); - - // sort by name - Comparator comparator = new Comparator() + private void displayGoals( boolean pluginOnly, String plugin, Set goals ) + { + Map map = new HashMap(); + for ( Iterator i = goals.iterator(); i.hasNext(); ) { - public int compare( Object o1, Object o2 ) + String goal = ( String ) i.next(); + String pluginName = ""; + int index = goal.indexOf( ':' ); + if ( index >= 0 ) { - String g1 = (String) o1; - String g2 = (String) o2; - return g1.compareTo( g2 ); + pluginName = goal.substring( 0, index ); } - }; - Collections.sort( list, comparator ); - - int defaultWrapIndent = 30; - int wrapIndent = defaultWrapIndent; - String msgPrefix = ""; - boolean hasDesc = false; - boolean firstLine = true; - List undocumentedGoals = new ArrayList(); - String lastPluginName = ""; - - for ( Iterator i = list.iterator(); i.hasNext();) - { - String goalName = (String) i.next(); // goal name - String goalDescription = mavenSession.getGoalDescription( goalName ); - - StringTokenizer st = new StringTokenizer( goalName, ":" ); - String pluginName = st.nextToken(); //ex. java - boolean newPlugin = !pluginName.equals( lastPluginName ); - - if ( pluginOnly ) + List l = ( List ) map.get( pluginName ); + if ( l == null ) { - if ( plugin == null && !pluginName.equals( goalName ) ) - { - // only show default goal - continue; - } - else if ( plugin != null && !pluginName.equals( plugin ) ) - { - // only show specified plugin - continue; - } - } - - // Prepare the description - if ( goalDescription == null || goalDescription.equals( "null" ) ) - { - // If description equals the string "null", it came from the cache - hasDesc = false; - } - else - { - goalDescription = goalDescription.trim(); - hasDesc = goalDescription.length() != 0; + l = new ArrayList(); + map.put( pluginName, l ); } + l.add( goal.substring( index + 1 ) ); + } - if ( newPlugin ) + List goalList = ( List ) map.get( "" ); + if ( goalList != null ) + { + for ( Iterator i = goalList.iterator(); i.hasNext(); ) { - // starting new section - lastPluginName = pluginName; - - if ( st.hasMoreTokens() ) + String goal = ( String ) i.next(); + if ( map.containsKey( goal ) ) { - // uh oh we have no default goal defined - wrapIndent = defaultWrapIndent; - msgPrefix = " " + st.nextToken() + " "; - if ( msgPrefix.trim().length() == 0 ) + i.remove(); + List pluginGoals = ( List ) map.get( goal ); + if ( pluginGoals == null ) { - msgPrefix = " ( NO GOAL NAME ) "; + pluginGoals = new ArrayList(); + map.put( goal, pluginGoals ); } - msgPrefix = format( msgPrefix, wrapIndent, '.' ) + " "; - // +1 pad for space on the end - wrapIndent++; - - // if the goal found for a new plugin is not the default - // goal, then we make the plugin description something that - // makes clear there is no default goal defined - msgPrefix = pluginName - + "] ( NO DEFAULT GOAL )" + LS - + msgPrefix; + // don't add the empty goal as we always attempt to display the default } - else - { - // we are dealing with the default goal - msgPrefix = pluginName + "]"; + } + } - if ( hasDesc ) - { - msgPrefix += " : "; - wrapIndent = msgPrefix.length(); - } - } + List keys = new ArrayList( map.keySet() ); + Collections.sort( keys ); - // the first line of display goals should not start a new line - if ( firstLine || pluginOnly ) - { - msgPrefix = "[" + msgPrefix; - firstLine = false; - } - else - { - msgPrefix = LS + "[" + msgPrefix; - } + List undocumentedGoals = new ArrayList(); - if ( hasDesc ) + for ( Iterator i = keys.iterator(); i.hasNext(); ) + { + String pluginName = ( String ) i.next(); + if ( pluginOnly ) + { + if ( plugin == null ) { - System.out.println( msgPrefix - + wrapConsoleMessage( goalDescription, wrapIndent, - CONSOLE_WIDTH ) ); + // only show default goal + displayDefaultGoal( pluginName, mavenSession.getGoalDescription( pluginName ), false ); + continue; } - else + else if ( !pluginName.equals( plugin ) ) { - System.out.println( msgPrefix + " ( NO DESCRIPTION ) " ); + continue; } } - else + + displayDefaultGoal( pluginName, mavenSession.getGoalDescription( pluginName ), true ); + List l = ( List ) map.get( pluginName ); + Collections.sort( l ); + for ( Iterator j = l.iterator(); j.hasNext(); ) { - if ( hasDesc ) + String goalName = ( String ) j.next(); + String fullGoalName = pluginName.length() == 0 ? goalName : pluginName + ":" + goalName; + String goalDescription = mavenSession.getGoalDescription( fullGoalName ); + if ( goalDescription != null ) { - wrapIndent = defaultWrapIndent; // where the indent begins for all goals - // protect against a plugin called "plugin:" - if ( st.hasMoreTokens() ) - { - // not a new plugin so also not the first line of - // plugin section and not a goal with just a plugin - // name; there must be nextToken - msgPrefix = " " + st.nextToken() + " "; - //protect against empty goal names - if ( msgPrefix.trim().length() == 0 ) - { - msgPrefix = " ( NO GOAL ) "; - } - - } - else - { - msgPrefix = " ( NO GOAL ) "; - } - - msgPrefix = format( msgPrefix, wrapIndent, '.' ) + " "; - wrapIndent += 1; // for spaces used to pad the prefix - - log.info( msgPrefix + wrapConsoleMessage( goalDescription, wrapIndent, CONSOLE_WIDTH ) ); + displayGoal( goalName, goalDescription ); } else { - undocumentedGoals.add( goalName ); + undocumentedGoals.add( fullGoalName ); } } } @@ -1021,6 +963,39 @@ log.info( "" ); } + private void displayGoal( String goalName, String goalDescription ) + { + if ( "".equals( goalName ) ) + { + goalName = "( NO GOAL )"; + } + + String msgPrefix = format( " " + goalName + " ", WRAP_INDENT, '.' ) + " "; + + log.info( msgPrefix + wrapConsoleMessage( goalDescription, WRAP_INDENT + 1, CONSOLE_WIDTH ) ); + } + + private void displayDefaultGoal( String goalName, String goalDescription, boolean newLine ) + { + if ( "".equals( goalName ) ) + { + return; + } + + String msgPrefix = format( "[" + goalName + "]", WRAP_INDENT, ' ' ) + " "; + if ( goalDescription == null ) + { + goalDescription = "( NO DEFAULT GOAL )"; + } + + if ( newLine ) + { + msgPrefix = LS + msgPrefix; + } + + log.info( msgPrefix + wrapConsoleMessage( goalDescription, WRAP_INDENT + 1, CONSOLE_WIDTH ) ); + } + /** Display goals without descriptions. * * @param list List of undocument goal names. @@ -1046,7 +1021,7 @@ * @param pad The trailing pad character. * * @return The formatted string, or the original string - * if the length is already >= <code>width</code>. + * if the length is already >= <code>width</code>. */ protected String format( String orig, int width, @@ -1083,6 +1058,27 @@ int wrapIndent, int lineWidth ) { + if ( msg.indexOf( '\n' ) < 0 && msg.indexOf( '\r' ) < 0 ) + { + return wrapConsoleLine( msg, wrapIndent, lineWidth ); + } + + StringBuffer buf = new StringBuffer(); + StringTokenizer tok = new StringTokenizer( msg, "\n\r" ); + while ( tok.hasMoreTokens() ) + { + String token = tok.nextToken().trim(); + if ( token.length() > 0 ) + { + buf.append( wrapConsoleLine( token, wrapIndent, lineWidth ) ); + buf.append( LS ); + } + } + return buf.toString(); + } + + private String wrapConsoleLine( String msg, int wrapIndent, int lineWidth ) + { int offset = lineWidth - wrapIndent; if ( msg.length() <= offset ) { @@ -1110,6 +1106,7 @@ currentPos = bIter.preceding( offset ); // append from the start to currentPos buf.append( msg.substring( 0, currentPos ) ); + // start next line buf.append( LS ); @@ -1176,8 +1173,9 @@ * To allow subclasses stop the app from exiting * @param status the value to exit with */ - protected void exit(int status) + protected void exit( int status ) { - System.exit(status); + System.exit( status ); } } + No revision No revision 1.3.4.13 +14 -1 maven/src/java/org/apache/maven/plugin/GoalToJellyScriptHousingMapper.java Index: GoalToJellyScriptHousingMapper.java =================================================================== RCS file: /home/cvs/maven/src/java/org/apache/maven/plugin/GoalToJellyScriptHousingMapper.java,v retrieving revision 1.3.4.12 retrieving revision 1.3.4.13 diff -u -r1.3.4.12 -r1.3.4.13 --- GoalToJellyScriptHousingMapper.java 28 Mar 2004 21:27:27 -0000 1.3.4.12 +++ GoalToJellyScriptHousingMapper.java 21 Apr 2004 00:33:22 -0000 1.3.4.13 @@ -446,7 +446,20 @@ String getGoalDescription( String goalName ) { Goal goal = goalProject.getGoal( goalName ); - return goal != null ? goal.getDescription() : ""; + String goalDescription = ( goal != null ? goal.getDescription() : null ); + if ( goalDescription != null ) + { + goalDescription = goalDescription.trim(); + if ( "null".equals( goalDescription ) ) + { + goalDescription = null; + } + else if ( goalDescription.length() == 0 ) + { + goalDescription = null; + } + } + return goalDescription; } void addResolvedPlugins( List projectHousings ) 1.70.4.39 +42 -19 maven/src/java/org/apache/maven/plugin/PluginManager.java Index: PluginManager.java =================================================================== RCS file: /home/cvs/maven/src/java/org/apache/maven/plugin/PluginManager.java,v retrieving revision 1.70.4.38 retrieving revision 1.70.4.39 diff -u -r1.70.4.38 -r1.70.4.39 --- PluginManager.java 16 Apr 2004 07:00:14 -0000 1.70.4.38 +++ PluginManager.java 21 Apr 2004 00:33:22 -0000 1.70.4.39 @@ -128,7 +128,7 @@ private final PluginCacheManager cacheManager = new PluginCacheManager(); /** Goal to Plugins mapper. */ - private final GoalToJellyScriptHousingMapper mapper = new GoalToJellyScriptHousingMapper(); + private GoalToJellyScriptHousingMapper mapper = new GoalToJellyScriptHousingMapper(); /** Current plugins mapper (transient - includes maven.xml, etc). **/ private GoalToJellyScriptHousingMapper transientMapper = mapper; @@ -436,6 +436,27 @@ project.getContext().setClassLoader( projectClassLoader ); } + List readMavenXml( Project project, GoalToJellyScriptHousingMapper mapper ) throws MavenException + { + Project p = project; + List projectHousings = new ArrayList(); + + // Project's Jelly script + while ( p != null ) + { + if ( p.hasMavenXml() ) + { + File mavenXml = p.getMavenXml(); + + JellyScriptHousing jellyScriptHousing = createJellyScriptHousing( project, mavenXml ); + jellyScriptHousing.parse( mapper ); + projectHousings.add( jellyScriptHousing ); + } + p = p.getParent(); + } + return projectHousings; + } + /** * Attain the goals. * @@ -491,8 +512,6 @@ // may wish to override so we guarantee precedence of the goals by running the jelly scripts // in the above mentioned order. - List projectHousings = new ArrayList(); - // driver.jelly InputStream driver = getClass().getResourceAsStream( "/driver.jelly" ); JellyScriptHousing driverHousing = createJellyScriptHousing( project, driver ); @@ -502,21 +521,7 @@ driverHousing.parse( transientMapper, driver ); driver.close(); - Project p = project; - - // Project's Jelly script - while ( p != null ) - { - if ( p.hasMavenXml() ) - { - File mavenXml = p.getMavenXml(); - - JellyScriptHousing jellyScriptHousing = createJellyScriptHousing( project, mavenXml ); - jellyScriptHousing.parse( transientMapper ); - projectHousings.add( jellyScriptHousing ); - } - p = p.getParent(); - } + List projectHousings = readMavenXml( project, transientMapper ); if ( goals != null ) { @@ -710,6 +715,19 @@ } /** + * Warning - this completely scrogs the default mapper. Only use this before System.exit! + * (currently used by maven -u). + * @todo refactor to return mapper instead and use that, or perhaps instantiate a new plugin manager + * @return + */ + public Set getGoalNames( Project project ) throws MavenException + { + mapper = new GoalToJellyScriptHousingMapper(); + readMavenXml( project, mapper ); + return mapper.getGoalNames(); + } + + /** */ public void installPlugin( File file, Project parentProject ) throws MavenException { @@ -893,5 +911,10 @@ context.removeVariable( PLUGIN_HOUSING ); } + public Project getPluginProjectFromGoal( String goal ) throws MavenException + { + JellyScriptHousing housing = mapper.getPluginHousing( goal ); + return housing != null ? housing.getProject() : null; + } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]