You are viewing a plain text version of this content. The canonical link for it is here.
Posted to m2-dev@maven.apache.org by ca...@apache.org on 2005/02/23 21:57:34 UTC

cvs commit: maven-components/maven-repository-tools pom.xml

carlos      2005/02/23 12:57:34

  Modified:    maven-repository-tools pom.xml
  Added:       maven-repository-tools/src/main/java/org/apache/maven/repository
                        ChecksumValidator.java RepositoryTools.java
               maven-repository-tools/src/test/java/org/apache/maven/repository
                        RepositoryToolsTest.java
  Log:
  Added checksum validator repository tool
  
  Revision  Changes    Path
  1.1                  maven-components/maven-repository-tools/src/main/java/org/apache/maven/repository/ChecksumValidator.java
  
  Index: ChecksumValidator.java
  ===================================================================
  package org.apache.maven.repository;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import java.io.File;
  import java.util.Iterator;
  import java.util.List;
  
  import org.apache.maven.artifact.Artifact;
  import org.apache.maven.artifact.repository.ArtifactRepository;
  
  /**
   * Prints all artifacts without checksum file
   * 
   * @todo generate checksums for those files without it
   * 
   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez </a>
   * @version $Id: ChecksumValidator.java,v 1.1 2005/02/23 20:57:34 carlos Exp $
   */
  
  public class ChecksumValidator
  {
  
      public static void main( String[] args )
      {
          if ( args.length != 1 )
          {
              System.out.println( "Usage: " + ChecksumValidator.class.getName() + " path.to.local.repository" );
  
              return;
          }
  
          ArtifactRepository localRepository = new ArtifactRepository();
  
          String path = args[0];
  
          File f = new File( path );
  
          localRepository.setUrl( "file://" + f.getPath() );
  
          List artifacts = RepositoryTools.getAllArtifacts( localRepository );
  
          Iterator it = artifacts.iterator();
  
          while ( it.hasNext() )
          {
              Artifact artifact = (Artifact) it.next();
  
              if ( !artifact.getChecksumFile().exists() )
              {
                  System.out.println( artifact );
              }
          }
      }
  
  }
  
  
  
  1.1                  maven-components/maven-repository-tools/src/main/java/org/apache/maven/repository/RepositoryTools.java
  
  Index: RepositoryTools.java
  ===================================================================
  package org.apache.maven.repository;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import java.io.File;
  import java.io.FilenameFilter;
  import java.util.LinkedList;
  import java.util.List;
  
  import org.apache.maven.artifact.Artifact;
  import org.apache.maven.artifact.DefaultArtifact;
  import org.apache.maven.artifact.repository.ArtifactRepository;
  
  /**
   * Tools to help repository management
   * 
   * @author <a href="mailto:carlos@apache.org">Carlos Sanchez </a>
   * @version $Id: RepositoryTools.java,v 1.1 2005/02/23 20:57:34 carlos Exp $
   */
  
  public class RepositoryTools
  {
  
      /**
       * Files with these extensions will be ignored
       */
      private static final String[] IGNORE_EXTENSIONS = { "md5" };
  
      /**
       * Get all artifacts present in a file repository
       * 
       * @todo make this layout independent
       * 
       * @param artifactRepository
       *            all files in this repository basedir will be returned as
       *            artifacts
       * @return all the artifacts available in the repository
       */
      public static List getAllArtifacts( ArtifactRepository artifactRepository )
      {
          List list = new LinkedList();
  
          File basedir = new File( artifactRepository.getBasedir() );
  
          File[] groups = basedir.listFiles();
  
          for ( int i = 0; (groups != null) && (i < groups.length); i++ )
          {
              File[] types = groups[i].listFiles();
  
              for ( int j = 0; (types != null) && (j < types.length); j++ )
              {
  
                  File[] artifacts = types[j].listFiles( new FilenameFilter() {
  
                      public boolean accept( File dir, String name )
                      {
                          int x = name.lastIndexOf( "." );
  
                          String extension = name.substring( x, name.length() );
  
                          for ( int y = 0; y < IGNORE_EXTENSIONS.length; y++ )
                          {
  
                              if ( extension.equals( IGNORE_EXTENSIONS[y] ) )
  
                                  return false;
                          }
  
                          return true;
                      }
                  } );
  
                  for ( int k = 0; (artifacts != null) && (k < artifacts.length); k++ )
                  {
                      Artifact artifact = getArtifact( groups[i].getName(), types[j].getName().substring( 0,
                          types[j].getName().length() - 1 ), artifacts[k] );
  
                      if ( artifact != null )
  
                          list.add( artifact );
                  }
              }
          }
  
          return list;
      }
  
      /**
       * Construct an artifact object from a file
       * 
       * @todo make this layout independent
       * 
       * @param groupId
       * @param type
       * @param artifactFile
       *            artifactId, version and path will be get from this file name
       * @return
       */
      public static Artifact getArtifact( String groupId, String type, File artifactFile )
      {
          String name = artifactFile.getName();
  
          int i = name.lastIndexOf( "-" );
  
          int j = name.lastIndexOf( "." );
  
          if ( (i < 0) || (j < 0) )
          {
              return null;
          }
  
          String version = name.substring( i + 1, j );
  
          String artifactId = name.substring( 0, i );
  
          Artifact artifact = new DefaultArtifact( groupId, artifactId, version, type );
  
          artifact.setPath( artifactFile.getPath() );
  
          return artifact;
      }
  
  }
  
  
  
  1.1                  maven-components/maven-repository-tools/src/test/java/org/apache/maven/repository/RepositoryToolsTest.java
  
  Index: RepositoryToolsTest.java
  ===================================================================
  /*
   * Copyright 2004 Carlos Sanchez.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.maven.repository;
  
  import java.io.File;
  import java.util.List;
  
  import org.apache.maven.artifact.Artifact;
  import org.apache.maven.artifact.ArtifactComponentTestCase;
  import org.apache.maven.repository.RepositoryTools;
  
  /**
   * @author Carlos Sanchez
   * @version $Revision: 1.1 $
   */
  public class RepositoryToolsTest
      extends ArtifactComponentTestCase
  {
  
      protected void setUp() throws Exception
      {
          super.setUp();
      }
  
      protected String component()
      {
          return "repositorytools";
      }
  
      public void testGetAllArtifacts() throws Exception
      {
          Artifact artifact = createLocalArtifact( "a", "1.0" );
          List artifacts = RepositoryTools.getAllArtifacts( localRepository() );
          assertEquals( 1, artifacts.size() );
          assertEquals( artifact, artifacts.get( 0 ) );
      }
  
      public void testGetArtifact()
      {
          File artifactFile = new File( "whatever/maven/jars/maven-meeper-1.0.jar" );
          Artifact artifact = RepositoryTools.getArtifact( "maven", "jar", artifactFile );
          assertEquals( "maven", artifact.getGroupId() );
          assertEquals( "maven-meeper", artifact.getArtifactId() );
          assertEquals( "1.0", artifact.getVersion() );
          assertEquals( "jar", artifact.getType() );
          assertNotNull( artifact.getPath() );
      }
  
  }
  
  
  
  1.3       +5 -0      maven-components/maven-repository-tools/pom.xml
  
  Index: pom.xml
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-repository-tools/pom.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- pom.xml	31 Aug 2004 11:47:13 -0000	1.2
  +++ pom.xml	23 Feb 2005 20:57:34 -0000	1.3
  @@ -36,5 +36,10 @@
         <artifactId>plexus</artifactId>
         <version>0.17-SNAPSHOT</version>
       </dependency>
  +    <dependency>
  +      <groupId>maven</groupId>
  +      <artifactId>maven-artifact</artifactId>
  +      <version>2.0-SNAPSHOT</version>
  +    </dependency>
     </dependencies>
   </project>