View Javadoc

1   package org.apache.maven.continuum.xmlrpc.client;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.continuum.xmlrpc.project.BuildResult;
23  import org.apache.maven.continuum.xmlrpc.project.BuildResultSummary;
24  import org.apache.maven.continuum.xmlrpc.project.ProjectGroupSummary;
25  import org.apache.maven.continuum.xmlrpc.project.ProjectSummary;
26  
27  import java.net.URL;
28  import java.util.Date;
29  import java.util.List;
30  
31  /**
32   * Utility class to purge old build results.
33   * <p/>
34   * The easiest way to use it is to change the exec plugin config in the pom to execute this class instead of
35   * SampleClient, change RETENTION_DAYS if desired, and type 'mvn clean install exec:exec'
36   */
37  public class BuildResultsPurge
38  {
39  
40      private static ContinuumXmlRpcClient client;
41  
42      private static long RETENTION_DAYS = 60;
43  
44      private static long DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
45  
46      public static void main( String[] args )
47          throws Exception
48      {
49  
50          client = new ContinuumXmlRpcClient( new URL( args[0] ), args[1], args[2] );
51  
52          long today = new Date().getTime();
53  
54          System.out.println( "Today is " + new Date( today ) );
55  
56          long purgeDate = today - ( RETENTION_DAYS * DAY_IN_MILLISECONDS );
57          //long purgeDate = today - 1000;  // 1 second ago (for testing)
58  
59          System.out.println( "Purging build results older than " + new Date( purgeDate ) );
60  
61          List<ProjectGroupSummary> groups = client.getAllProjectGroups();
62  
63          for ( ProjectGroupSummary group : groups )
64          {
65  
66              System.out.println( "Project Group [" + group.getId() + "] " + group.getName() );
67  
68              List<ProjectSummary> projects = client.getProjects( group.getId() );
69  
70              for ( ProjectSummary project : projects )
71              {
72  
73                  System.out.println( " Project [" + project.getId() + "] " + project.getName() );
74  
75                  List<BuildResultSummary> results = client.getBuildResultsForProject( project.getId() );
76  
77                  for ( BuildResultSummary brs : results )
78                  {
79  
80                      BuildResult br = client.getBuildResult( project.getId(), brs.getId() );
81  
82                      System.out.print( "  Build Result [" + br.getId() + "] ended " + new Date( br.getEndTime() ) );
83  
84                      if ( br.getEndTime() > 0 && br.getEndTime() < purgeDate )
85                      {
86  
87                          client.removeBuildResult( br );
88                          System.out.println( " ...removed." );
89                      }
90                      else
91                      {
92                          System.out.println( " ...retained." );
93                      }
94                  }
95              }
96  
97          }
98      }
99  }