2014-07-25 63 views
0

我們使用Atlassian Jira戰爭並定製它,然後部署戰爭。螞蟻是建立它的首選和建議的方式。但Jenkins構建花費了大量時間,我們希望使用更好的方法來進行依賴關係管理。我們使用apache archiva作爲我們的存儲庫。從ANT到Jira的Maven構建

早些時候我計劃獲得jar校驗和並構建一個POM,然後對其進行自定義。 https://github.com/myspotontheweb/ant2ivy/blob/master/ant2ivy.groovy

Getting a File's MD5 Checksum in Java

這種做法顯得有點容易出錯,所以我現在決定直接讀取pom.properties從罐子文件,並得到使用MVN依賴性detials:獲取插件。

我的問題是: 如果我用jar讀取並創建一個POM,它將是一長串依賴條目。有沒有更好的方式來處理它像一個真正的POM文件與父母和孩子的依賴。我們希望這一點對於每個構建/升級都很困難。或者只是一些更好的想法請。

+0

我是ant2ivy腳本的作者。使用它有沒有問題?如果您已經在使用ANT並擁有Archiva存儲庫,那麼使用ivy來管理您的依賴關係是最好的選擇。 –

+0

@Mark,我已閱讀您對論壇和校驗和方法的所有評論,並且您的代碼很好。它的尖銳,我在Archiva 1.3.5不支持休息電話和提供的網絡服務..我會盡力爲他們最後。我可能要面對的問題是,每一個新版本的Jira,都可能會有新的jar,並且在獲得校驗和時會給我帶來麻煩。所以我打算使用java.util.jar.JarEntry和java.util.jar.JarFile讀取lib文件夾和pom.properties文件。我能夠做到這一點,現在我有我需要的所有細節和Java。所以它更容易.. – kumarn

+0

啊...是的ant2ivy腳本旨在利用Sonatype Nexus REST API調用(從未考慮使用Archiva)。在這種情況下祝你好運,抱歉我的代碼無法幫到你。 –

回答

0
import java.lang.reflect.InvocationTargetException; 

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 

import org.apache.commons.beanutils.PropertyUtils; 
import org.apache.commons.lang.builder.ReflectionToStringBuilder; 

@XmlRootElement(name="artifact") 
public class Artifact { 

    private String groupId; 
    private String artifactName; 
    private String version; 

    public Artifact(final String groupId,final String artifactName,final String version) { 
     super(); 
     this.groupId = groupId; 
     this.artifactName = artifactName; 
     this.version = version; 
    } 

    public Artifact(final String artifactName,final String version) { 
     super(); 
     this.artifactName = artifactName; 
     this.version = version; 
    } 

    public Artifact() {} 

    @XmlJavaTypeAdapter(CollapsedStringAdapter.class) 
    @XmlElement(name="groupId") 
    public String getGroupId() { 
     return groupId; 
    } 

    public void setGroupId(final String groupId) { 
     this.groupId = groupId; 
    } 
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class) 
    @XmlElement(name="artifactId") 
    public String getArtifactName() { 
     return artifactName; 
    } 

    public void setArtifactName(final String artifactName) { 
     this.artifactName = artifactName; 
    } 
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class) 
    @XmlElement(name="version") 
    public String getVersion() { 
     return version; 
    } 

    public void setVersion(final String version) { 
     this.version = version; 
    } 

    @Override 
    public String toString() { 
     return ReflectionToStringBuilder.toString(this); 
    } 
    @Override 
    public boolean equals(final Object other){ 

     try { 
      if(PropertyUtils.getProperty(other, groupId).equals(PropertyUtils.getProperty(this,groupId)) 
        && PropertyUtils.getProperty(other, version).equals(PropertyUtils.getProperty(this,version)) 
        && PropertyUtils.getProperty(other, artifactName).equals(PropertyUtils.getProperty(this,artifactName))) { 

       return true; 
      } 
     } catch (IllegalAccessException e) { 
      return false; 
     } catch (InvocationTargetException e) { 
      return false; 
     } catch (NoSuchMethodException e) { 
      return false; 
     } 
     return false; 
    } 
    @Override 
    public int hashCode() { 
     return groupId.hashCode()^artifactName.hashCode()^version.hashCode(); 
    } 
} 
-------------------------------- 


import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Enumeration; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Properties; 
import java.util.Set; 
import java.util.jar.JarEntry; 
import java.util.jar.JarFile; 

import org.apache.commons.io.FilenameUtils; 
import org.springframework.web.client.RestTemplate; 

import artifact.model.Artifact; 
import artifact.model.SearchNGResponse; 

/** 
* Lucene Search ref: https://repository.sonatype.org/nexus-indexer-lucene-plugin/default/docs/path__lucene_search.html 
* Repo Url used for checksums : http://repository.opencastproject.org/nexus/content/repositories/public 
* e.g. - http://repository.opencastproject.org/nexus/service/local/lucene/search?a={artifactName}&v={artifactVersion}" 
* 
* Class to search artifact/jar GAV coordinates by reading the pom.properties file or their checksums. 
*/ 


import java.util.ArrayList; 
import java.util.List; 

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlElementWrapper; 
import javax.xml.bind.annotation.XmlRootElement; 


@XmlRootElement(name="searchNGResponse") 
public class SearchNGResponse { 

    private List<Artifact> artifacts = new ArrayList<Artifact>(); 

    @XmlElementWrapper(name ="data") 
    @XmlElement(name ="artifact") 
    public List<Artifact> getArtifacts(){ 
     return artifacts; 
    } 
} 

----------------------- 

public abstract class ArtifactDetailsExtractor { 

    public abstract void generateMavenScript(final List<Artifact> artifact); 
    public abstract void generateIvyScript(final List<Artifact> artifact); 
    public abstract void generateGradleScript(final List<Artifact> artifact); 

    private static final String searchRepo2Location ="http://repository.opencastproject.org/nexus/service/local/lucene/search?sha1={checksumString}"; 

    public enum ArtifactParams{ 
     GROUPID("groupId"), 
     ARTIFACTID("artifactId"), 
     VERSION("version"), 
     JAR_EXTENSION("jar"), 
     PROPERTIES_EXTENSION("pom.properties"); 

     private String name; 

     public String getName() { 
      return name; 
     } 
     public void setName(String name) { 
      this.name = name; 
     } 
     private ArtifactParams(final String name) { 
      this.name=name; 
     }   
    } 

    public List<Artifact> generateArtifactList(final String directory) throws Exception { 

     final File folder = new File(directory); 
     final File[] files = folder.listFiles(); 


     final Set<File> allArtifacts = new HashSet<File>(); 
     for (final File artifactLoaded : files) { 
      final String jarFileExtension= FilenameUtils.getExtension(artifactLoaded.getName()); 

       if (artifactLoaded.isFile() && artifactLoaded.canRead() 
         && (jarFileExtension.equalsIgnoreCase(ArtifactParams.JAR_EXTENSION.getName()))) { 
        allArtifacts.add(artifactLoaded); 
       } 
     } 

     final List<Artifact> artifacts=generateAndFindJar(allArtifacts);   
     return Collections.unmodifiableList(artifacts); 
    } 

    private List<Artifact> generateAndFindJar(final Set<File> missingArtifacts) throws IOException { 

     final List<Artifact> artifacts = new ArrayList<Artifact>(); 

     final Set<File> availableArtifacts = new HashSet<File>(); 
     final RestTemplate restTemplate = new RestTemplate(); 
     for(File f: missingArtifacts) { 
      final InputStream is = new FileInputStream(f); 
      final String checksumString = org.apache.commons.codec.digest.DigestUtils.sha1Hex(is); 
      final SearchNGResponse result = restTemplate.getForObject(searchRepo2Location, SearchNGResponse.class, checksumString); 
      if(result!=null && !result.getArtifacts().isEmpty()) { 
       artifacts.add(result.getArtifacts().get(0));  
       availableArtifacts.add(f); 
      }else {  
       final Set<String> groupDetails = new HashSet<String>(); 
       final JarFile jarFile = new JarFile(f);     
       final Enumeration<JarEntry> entries = jarFile.entries(); 

       while (entries.hasMoreElements()) { 
        final JarEntry entry = entries.nextElement(); 

        if(entry.getName().contains(ArtifactParams.PROPERTIES_EXTENSION.getName())) { 
         final Properties artifactProp = new Properties(); 
         final InputStream input = jarFile.getInputStream(entry); 
         artifactProp.load(input); 
         groupDetails.add(artifactProp.getProperty(ArtifactParams.GROUPID.getName())); 
         final Artifact artifact = new Artifact(artifactProp.getProperty(ArtifactParams.GROUPID.getName()), 
           artifactProp.getProperty(ArtifactParams.ARTIFACTID.getName()), 
           artifactProp.getProperty(ArtifactParams.VERSION.getName())); 
         artifacts.add(artifact); 
         availableArtifacts.add(f); 
        } 
       }     
       jarFile.close(); 

      }      
     } 
     missingArtifacts.removeAll(availableArtifacts); 
     generateUnavailableArtifactList(missingArtifacts); 

     return Collections.unmodifiableList(artifacts); 
    } 

    private void generateUnavailableArtifactList(final Set<File> badArtifacts) { 
     System.out.println("number of jar files failed  : "+badArtifacts.size()); 
     try { 
      final PrintWriter writer = new PrintWriter(new OutputStreamWriter(
         new FileOutputStream("artifact.txt"), "utf-8")); 
      for(final File artifact: badArtifacts) { 
       writer.println(artifact.getName()); 
       System.out.println("jar files failed ----- : "+artifact.getName()); 
      } 
      writer.close(); 

     } catch (final IOException ex) { 
      ex.printStackTrace(); 
      System.err.println("Exception while finding unavailable dependencies , must exit process : " + ex.getMessage()); 
      System.exit(1); 
     } 
    } 

} 

這就是我如何設法將所有依賴關係從Ant lib文件夾獲取到gradle格式構建腳本。很少有其他的方式在這裏太..

http://gradle.1045684.n5.nabble.com/Gradle-Make-a-3rd-party-jar-available-to-local-gradle-repository-td1431953.html