You are viewing a plain text version of this content. The canonical link for it is here.
Posted to wsrf-commits@ws.apache.org by sc...@apache.org on 2005/10/22 05:55:22 UTC

svn commit: r327630 - in /webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin: ./ plugin.jelly plugin.properties project.properties project.xml src/ src/MavenEclipseClasspathUpdater.java

Author: scamp
Date: Fri Oct 21 20:55:10 2005
New Revision: 327630

URL: http://svn.apache.org/viewcvs?rev=327630&view=rev
Log:
the start of a maven eclipse .classpath updater plugin

Added:
    webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/
    webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/plugin.jelly
    webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/plugin.properties
    webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/project.properties
    webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/project.xml
    webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/src/
    webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/src/MavenEclipseClasspathUpdater.java

Added: webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/plugin.jelly
URL: http://svn.apache.org/viewcvs/webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/plugin.jelly?rev=327630&view=auto
==============================================================================
--- webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/plugin.jelly (added)
+++ webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/plugin.jelly Fri Oct 21 20:55:10 2005
@@ -0,0 +1,27 @@
+<?xml version="1.0"?>
+
+<project xmlns:ant="jelly:ant" xmlns:define="jelly:define"
+  xmlns:eclipseupdater="eclipseupdater">
+
+
+<define:taglib uri="eclipseupdater">
+    
+    <define:jellybean
+      name="eclipseupdater"
+      className="MavenEclipseClasspathUpdater"
+      method="update"
+      />
+ 
+</define:taglib> 
+  
+<goal name="eclipse-update" description="Updates Eclipses's .classpath file with the current SRC and API Dependencies">
+
+   <property name="lib" refid="maven.dependency.classpath"/>
+   <eclipseupdater:eclipseupdater
+        var="iu"
+        lib="${lib}" 
+        classpathFileLocation="${eclipse.classpath.file}"
+   />   
+   </goal>
+
+</project>
\ No newline at end of file

Added: webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/plugin.properties
URL: http://svn.apache.org/viewcvs/webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/plugin.properties?rev=327630&view=auto
==============================================================================
--- webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/plugin.properties (added)
+++ webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/plugin.properties Fri Oct 21 20:55:10 2005
@@ -0,0 +1,3 @@
+
+
+eclipse.classpath.file=
\ No newline at end of file

Added: webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/project.properties
URL: http://svn.apache.org/viewcvs/webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/project.properties?rev=327630&view=auto
==============================================================================
--- webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/project.properties (added)
+++ webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/project.properties Fri Oct 21 20:55:10 2005
@@ -0,0 +1,13 @@
+# -------------------------------------------------------------------
+# P R O J E C T  P R O P E R T I E S
+# -------------------------------------------------------------------
+
+# MAVEN CORE
+##############
+# comma-spearated list of remote JAR repository URLs
+maven.repo.remote = http://www.apache.org/dist/java-repository/, http://cvs.apache.org/repository/, http://mirrors.sunsite.dk/maven/, http://people.apache.org/~ips/maven-repo/
+
+# JAVA PLUGIN
+##############
+maven.compile.debug = on
+maven.compile.target = 1.4

Added: webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/project.xml
URL: http://svn.apache.org/viewcvs/webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/project.xml?rev=327630&view=auto
==============================================================================
--- webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/project.xml (added)
+++ webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/project.xml Fri Oct 21 20:55:10 2005
@@ -0,0 +1,63 @@
+<?xml version="1.0"?>
+
+<!-- Schema for this file is at: http://maven.apache.org/xsd/maven-project-3.0.0.xsd -->
+<!-- Reference for this file is at: http://maven.apache.org/reference/project-descriptor.html -->
+
+<project
+   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+   xsi:noNamespaceSchemaLocation="http://maven.apache.org/xsd/maven-project-3.0.0.xsd">
+
+  <pomVersion>3</pomVersion>
+  <id>maven-eclipse-classpath-plugin</id>
+  <name>Idea Plugin</name>
+  <currentVersion>1.0</currentVersion>
+  <description/>
+  <shortDescription/>
+  
+   <versions>
+     <version>
+       <id>1.0</id>
+       <name>1.0</name>
+       <tag>HEAD</tag>
+     </version>
+  </versions>
+  
+    <developers>
+      <developer>
+        <name>Sal Campana</name>
+        <id>scamp</id>
+        <email>sal.campana@hp.com</email>
+        <organization>Hewlett-Packard Company</organization>
+        <roles>
+          <role>Developer</role>
+        </roles>
+        <timezone>-5</timezone>
+      </developer>
+  </developers>  
+  
+  <dependencies>
+  <dependency>
+        <groupId>xalan</groupId>
+        <artifactId>xalan</artifactId>
+        <version>2.4.1</version>
+        <url>http://xml.apache.org/xalan-j/</url>
+        <properties>
+          <classloader>root.maven</classloader>
+        </properties>
+    </dependency>
+ </dependencies> 
+  
+  <build>
+  <sourceDirectory>src</sourceDirectory>
+  <resources>
+        <resource>
+          <directory></directory>
+          <includes>
+            <include>plugin.jelly</include>            
+            <include>plugin.properties</include>
+            <include>project.xml</include>
+          </includes>
+        </resource>
+    </resources>
+  </build>
+</project>

Added: webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/src/MavenEclipseClasspathUpdater.java
URL: http://svn.apache.org/viewcvs/webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/src/MavenEclipseClasspathUpdater.java?rev=327630&view=auto
==============================================================================
--- webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/src/MavenEclipseClasspathUpdater.java (added)
+++ webservices/wsrf/trunk/maven-plugins/maven-eclipse-classpath-plugin/src/MavenEclipseClasspathUpdater.java Fri Oct 21 20:55:10 2005
@@ -0,0 +1,376 @@
+/*=============================================================================*
+ *  Copyright 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.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+/**
+ * @author Sal Campana
+ */
+public class MavenEclipseClasspathUpdater
+{
+    private String m_classpathFileLocation;
+    private String m_lib;
+    private boolean m_isModified;
+
+    /**
+     * Component Element
+     */
+    private static final String CLASSPATH_ELEM = "classpath";
+
+    /**
+     * Name Attribute
+     */
+    private static final String NAME_ATTRIB = "name";
+
+    /**
+     * NewModuleRootManager name value
+     */
+    private static final String CLASSPATH_ENTRY_NODE = "classpathentry";
+
+    /**
+     * Type Attribute
+     */
+    private static final String KIND_ATTRIB = "kind";
+	private static final String PATH_ATTRIB = "path";
+    private Document m_originalDom;
+    private Document m_modifiableDom;
+    private File m_classpathFile;
+
+    /**
+     * Creates a new {@link MavenEclipseClasspathUpdater} object.
+     */
+    public MavenEclipseClasspathUpdater()
+    {
+        //set the system property to ensure the TransformerFactory is setup
+        System.setProperty("javax.xml.transform.TransformerFactory",
+                           "org.apache.xalan.processor.TransformerFactoryImpl");
+    }
+
+    /**
+     * Sets the IML file path
+     *
+     * @param classpathFile
+     */
+    public void setEclipseClasspathLocation(String classpathFile)
+    {
+        m_classpathFileLocation = classpathFile;
+    }
+
+    /**
+     * Sets the Maven-passed-in delimited String of dependencies.
+     *
+     * @param libvar DOCUMENT_ME
+     */
+    public void setLib(String libvar)
+    {
+        m_lib = libvar;
+    }
+
+    /**
+     * Returns the IML file path.
+     *
+     * @return Iml file path
+     */
+    public String getEclipseClasspathLocation()
+    {
+        return m_classpathFileLocation;
+    }
+
+    /**
+     * Returns the Maven-passed-in delimited dependency string/
+     *
+     * @return Maven-passed-in delimited dependency string
+     */
+    public String getLib()
+    {
+        return m_lib;
+    }
+
+    /**
+     * This operation reads in the IML file and determines if the Maven-passed-in dependencies match the dependencies in the
+     * IML.  If an entry was removed, then it is removed from the IML file.  If an entry is modified then the entry in the IML
+     * is modified.  If the Maven dependencies do not exist in the IML, then they are added to it.
+     *
+     * @throws ParserConfigurationException
+     * @throws IOException
+     * @throws SAXException
+     * @throws TransformerException
+     */
+    public void update()
+            throws IOException,
+                   ParserConfigurationException,
+                   SAXException,
+                   TransformerException
+    {
+
+        Map depMap;
+        m_isModified = false;
+
+        //get the Maven-defined dependencies parsed into a Map.
+        depMap = getDependencyMap();
+
+        if ((m_classpathFileLocation == null) || "".equals(m_classpathFileLocation))
+        {
+            throw new IllegalArgumentException("The .classpath path was null!  Please ensure you have set the System property "
+                                               + "\"eclipse.classpath.file\" to point to your Eclipse project's .classpath file.");
+        }
+
+        //load the doms for modifying and aintaining a backup.
+        m_classpathFile = setupEclipseClasspathDoms();
+
+        udateDependencies(m_classpathFile, depMap);
+
+        updateClasspathFile(m_classpathFile);
+    }
+
+    private void udateDependencies(File classpathFile, Map depMap)
+            throws IOException
+    {
+        //get <classpath> nodes
+        NodeList classpathElemNodes = m_modifiableDom.getElementsByTagName(CLASSPATH_ELEM);
+        int componentNodesLength = classpathElemNodes.getLength();
+
+        for (int m = 0; m < componentNodesLength; m++)
+        {
+            Node classpathNode = classpathElemNodes.item(m);
+
+            if (Node.ELEMENT_NODE == classpathNode.getNodeType())
+            {
+                Node classpathNameNode = classpathNode.getAttributes().getNamedItem(NAME_ATTRIB);
+
+                //find the classpathentry  <classpathentry>
+                if ((classpathNameNode != null)
+                    && classpathNameNode.getNodeValue().equals(CLASSPATH_ELEM))
+                {
+                    //get the <classpathentry> nodes
+                    NodeList classpathentryNodes = classpathNode.getChildNodes();
+
+                    for (int p = 0; p < classpathentryNodes.getLength(); p++)
+                    {
+                        Node classpathentry = classpathentryNodes.item(p);
+
+                        if (Node.ELEMENT_NODE == classpathentry.getNodeType())
+                        {
+                            if (CLASSPATH_ENTRY_NODE.equals(classpathentry.getNodeName()))
+                            {
+                                Node typeAttribute = classpathentry.getAttributes().getNamedItem(KIND_ATTRIB);
+                                
+                                Node pathNode = classpathentry.getAttributes().getNamedItem(PATH_ATTRIB);
+                                if(pathNode != null)
+                                {
+                                	String pathToDepJar = pathNode.getNodeValue();
+                                	
+                                	String depJarFileName = pathToDepJar.substring(pathToDepJar.lastIndexOf("/")+1);
+                                	String fileNameWithoutExtension = depJarFileName.substring(0, depJarFileName.lastIndexOf("."));
+                                	String depPathLocation = pathToDepJar.substring(0, pathToDepJar.lastIndexOf(depJarFileName));
+                                	
+                                //find module library
+                                if ((typeAttribute != null))
+                                {                                                                      
+                                        Node sourcePathNode = classpathentry.getAttributes().getNamedItem("sourcepath");
+                                        if(sourcePathNode == null)
+                                        {
+                                        	((Element)classpathentry).setAttribute("sourcepath", getExtendedDepUrl(pathToDepJar, "src"));
+                                        }                                    
+                                }
+                                
+                                //api jar is always the fill path..just differs if jar or dir
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private void updateClasspathFile(File classpathFile)
+            throws TransformerException, IOException
+    {
+        //write modifiableDom
+        if (m_isModified)
+        {
+            //backup the original file...
+
+            File tempFile =
+                    new File(m_classpathFileLocation + ".bak" );
+            writeFile(tempFile, m_originalDom);
+
+            //write the modified file....
+            writeFile(classpathFile, m_modifiableDom);
+            System.out.println("Update of Eclipse was successful!");
+        }
+        else
+        {
+            System.out.println("The Eclipse .classpath was up-to-date.  No changes were made.");
+        }
+    }
+
+    private File setupEclipseClasspathDoms()
+            throws SAXException, IOException, ParserConfigurationException
+    {
+        //setup File for Eclipse Classpath file...
+        File classpathFile = new File(m_classpathFileLocation);
+
+        if (!classpathFile.exists())
+        {
+            throw new IllegalArgumentException("The provided .classpath path: " + m_classpathFileLocation
+                                               + " does not exist, please check your settings.");
+        }
+
+        if (classpathFile.isDirectory())
+        {
+            throw new IllegalArgumentException("The provided .classpath path: " + m_classpathFileLocation
+                                               + " is a directory!, please check your settings and ensure you are pointing to the actual .classpath file.");
+        }
+
+        //hold a ref to the original file
+        m_originalDom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(classpathFile);
+
+        //get a reference to modify
+        m_modifiableDom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(classpathFile);
+        return classpathFile;
+    }
+    
+    private String getExtendedDepUrl( String mavenDepJar, String extension)
+    {
+        String firstHalfUrl = mavenDepJar.substring(0, mavenDepJar.lastIndexOf("-"));
+        String secondHalfUrl = mavenDepJar.substring(mavenDepJar.lastIndexOf("-"));
+        String apiJarURL = firstHalfUrl + "-" + extension + secondHalfUrl;
+
+        return apiJarURL;
+    }
+
+      /**
+     * Writes a DOM to the filesystem using the Xalan TransformerFactory.
+     *
+     * @param file
+     * @param dom
+     * @throws TransformerException
+     * @throws IOException
+     */
+    private void writeFile(File file,
+                           Document dom)
+            throws TransformerException,
+                   IOException
+    {
+        file.createNewFile();
+
+        // Use a Transformer for output
+        TransformerFactory tFactory = TransformerFactory.newInstance();
+        Transformer transformer = tFactory.newTransformer();
+
+        DOMSource source = new DOMSource(dom);
+        StreamResult result = new StreamResult(file);
+        transformer.transform(source, result);
+    }
+
+    /**
+     * Returns a map of Maven-passed-in dependencies for the project.
+     * <p/>
+     * The key is the jar path jarPath() and the value is the IntelliJ required jar url buildJarUrl()
+     *
+     * @return Dependency map.
+     */
+    private Map getDependencyMap()
+            throws IOException
+    {
+        if ((m_lib == null) || "".equals(m_lib))
+        {
+            throw new IllegalArgumentException("The Maven-passed-in dependency list was null!");
+        }
+
+        Map deps = new HashMap();
+        m_lib = m_lib.replace('\\', '/');
+        StringTokenizer tokenizer = new StringTokenizer(m_lib, File.pathSeparator);
+
+        while (tokenizer.hasMoreTokens())
+        {
+            String token = tokenizer.nextToken();
+            File f = new File(token);
+            deps.put(getJarPath(f.getCanonicalPath()),
+                     buildJarUrl(f.getCanonicalPath()));
+        }
+
+        return deps;
+    }
+
+    /**
+     * Returns the jar-path which is the path to the jar minus the "jar://" and -XXX.jar (version info) This is used to
+     * normalize the url string per jar file.
+     *
+     * @param url URL string to be modified
+     * @return jar pat minus "jar://" AND -XXX.jar (Maven Version info)
+     */
+    private String getJarPath(String url)
+    {
+        //strip off the -00....jar to ensure its the jar name without the version info
+        if (url.lastIndexOf("-") > 0)
+        {
+            url = url.substring(0,
+                                url.lastIndexOf("-"));
+
+            //remove if case with "jar://"
+            if (url.startsWith("jar"))
+            {
+                url = url.substring(6);
+            }
+        }
+
+        return url;
+    }
+
+    /**
+     * Builds the jar url as expected by the IntelliJ config.
+     *
+     * @param url
+     * @return
+     */
+    private String buildJarUrl(String url)
+    {
+        if (url.startsWith("jar"))
+        {
+            return url;
+        }
+        String newUrl = "jar://" + url;
+        if (!url.endsWith("!"))
+        {
+            newUrl = newUrl + "!/";
+        }
+        else
+        {
+            newUrl = newUrl + "/";
+        }
+        return newUrl;
+    }
+}
\ No newline at end of file