You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by fe...@apache.org on 2007/12/04 22:58:54 UTC

svn commit: r601101 - in /directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins: StudioCleanMojo.java StudioEclipseMojo.java

Author: felixk
Date: Tue Dec  4 13:58:50 2007
New Revision: 601101

URL: http://svn.apache.org/viewvc?rev=601101&view=rev
Log:
Add goals 'eclipse' and 'clean'

Added:
    directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java   (with props)
    directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java   (with props)

Added: directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java?rev=601101&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java (added)
+++ directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java Tue Dec  4 13:58:50 2007
@@ -0,0 +1,84 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.directory.studio.maven.plugins;
+
+import java.io.File;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * Clean stuff generated by studio:eclipse
+ * 
+ * @goal clean
+ * @execute phase="generate-resources"
+ * @description Clean stuff generated by studio:eclipse
+ * @requiresProject
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Id: StudioReplaceFileInJarMojo.java 350 2007-10-28 12:01:17Z felix $
+ */
+public class StudioCleanMojo extends AbstractMojo {
+
+	/**
+	 * POM
+	 * 
+	 * @parameter expression="${project}"
+	 * @readonly
+	 * @required
+	 */
+	protected MavenProject project;
+
+	public void execute() throws MojoExecutionException {
+		try {
+			File file = new File(project.getBasedir(), "maven-eclipse.xml");
+			getLog().info("Deleting " + file);
+			file.delete();
+			file = new File(project.getBasedir(), ".externalToolBuilder");
+			getLog().info("Deleting " + file);
+			deleteDirectory(file);
+			file = new File(project.getBasedir(), "lib");
+			getLog().info("Deleting " + file);
+			deleteDirectory(file);
+		} catch (Exception e) {
+			getLog().error(e);
+		}
+	}
+
+	/**
+	 * Delete a directory
+	 * 
+	 * @param path
+	 * @return
+	 */
+	private static boolean deleteDirectory(File path) {
+		if (path.exists()) {
+			for (File file : path.listFiles()) {
+				if (file.isDirectory()) {
+					deleteDirectory(file);
+				} else {
+					file.delete();
+				}
+			}
+		}
+		return (path.delete());
+	}
+}

Propchange: directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java
URL: http://svn.apache.org/viewvc/directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java?rev=601101&view=auto
==============================================================================
--- directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java (added)
+++ directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java Tue Dec  4 13:58:50 2007
@@ -0,0 +1,246 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.directory.studio.maven.plugins;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
+import org.codehaus.plexus.util.xml.XMLWriter;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
+import org.codehaus.plexus.util.xml.Xpp3DomWriter;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+/**
+ * Prepares for eclipse:
+ * Copy artifacts nonscoped "provided" to ${basedir}/lib
+ * Add artifacts nonscoped "provided" to Bundle-ClassPath ind MANIFEST.MF
+ * Adapt ${basedir}/.classpath for artifacts nonscoped "provided"
+ * 
+ * @goal eclipse
+ * @execute phase="generate-resources"
+ * @description Copy artifacts nonscoped "provided" to ${basedir}/lib,
+ *              Add artifacts nonscoped "provided" to Bundle-ClassPath ind MANIFEST.MF,
+ *              Adapt ${basedir}/.classpath for artifacts nonscoped "provided"
+ * @requiresProject
+ * @requiresDependencyResolution runtime
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Id: StudioReplaceFileInJarMojo.java 350 2007-10-28 12:01:17Z felix $
+ */
+public class StudioEclipseMojo extends AbstractMojo {
+
+	/**
+	 * POM
+	 * 
+	 * @parameter expression="${project}"
+	 * @readonly
+	 * @required
+	 */
+	protected MavenProject project;
+
+	/**
+	 * Constant used for newline.
+	 */
+	private static final String NEWLINE = "\n";
+
+	/**
+	 * Bundle-ClassPath: updated with the list of dependencies.
+	 */
+	public final static String ENTRY_BUNDLE_CLASSPATH = "Bundle-ClassPath:";
+
+	/**
+	 * classpathPrefix
+	 * 
+	 * @parameter expression="."
+	 */
+	private String classpathPrefix;
+
+	public void execute() throws MojoExecutionException {
+		try {
+			// Create list of used artifacts
+			final List<Artifact> artifactList = createArtifactList();
+
+			// copy Artifacts
+			copyArtifacts(artifactList);
+
+			// Update Bundle-Classpath in MANIFEST.MF
+			updateManifest(artifactList);
+
+			// Update .classpath
+			updateDotClasspath(artifactList);
+
+		} catch (Exception e) {
+			getLog().error(e);
+		}
+	}
+
+	/**
+	 * Return a list of artifacts nonscoped "provided"
+	 * @return
+	 */
+	private List<Artifact> createArtifactList() {
+		List<Artifact> list = new ArrayList<Artifact>();
+		for (Iterator<Artifact> artifactItem = project.getArtifacts()
+				.iterator(); artifactItem.hasNext();) {
+			Artifact artifact = (Artifact) artifactItem.next();
+			if (!artifact.getScope().equalsIgnoreCase("provided")) {
+				list.add(artifact);
+			}
+		}
+		return list;
+	}
+
+	/**
+	 * Copy artifacts to ${basedir}/lib
+	 * @param list
+	 * @throws IOException
+	 */
+	private void copyArtifacts(final List<Artifact> list) throws IOException {
+		// Only proceed when we have artifacts to process
+		if (!list.isEmpty()) {
+			final File copyDir = new File(project.getBasedir(), classpathPrefix);
+
+			if (!copyDir.exists())
+				copyDir.mkdirs();
+
+			for (Artifact artifact : list) {
+				final File destFile = new File(copyDir, artifact.getFile()
+						.getName());
+				FileUtils.copyFile(artifact.getFile(), destFile);
+				getLog().info(
+						"Copying " + artifact.getFile() + " to " + destFile);
+			}
+		}
+	}
+
+	/**
+	 * Updates the Bundle-ClassPath entry in the manifest file
+	 * @param list
+	 * @throws FileNotFoundException
+	 * @throws IOException
+	 */
+	private void updateManifest(final List<Artifact> list)
+			throws FileNotFoundException, IOException {
+		final File manifestFile = new File(project.getBasedir(),
+				"META-INF/MANIFEST.MF");
+		getLog().info("Update Bundle-Classpath in " + manifestFile);
+
+		// Build Bundle-ClassPath entry
+		final StringBuilder bundleClasspath = new StringBuilder(" .");
+		for (Artifact artifact : list) {
+			bundleClasspath.append(",").append(NEWLINE).append(" ").append(
+					classpathPrefix).append(File.separator).append(
+					artifact.getFile().getName());
+		}
+
+		boolean inBundleClasspathEntry = false;
+
+		// Read existing MANIFEST.MF and add existing entries
+		// to StringBuilder exept Bundle-ClassPath entry
+		StringBuilder manifestSb = new StringBuilder();
+		BufferedReader in = new BufferedReader(new InputStreamReader(
+				new FileInputStream(manifestFile), "UTF-8"));
+		String line;
+		while ((line = in.readLine()) != null) {
+			if (inBundleClasspathEntry && line.indexOf(":") > -1) {
+				inBundleClasspathEntry = false;
+			} else if (inBundleClasspathEntry) {
+				continue;
+			}
+
+			String name = line.substring(0, line.indexOf(":") + 1);
+
+			if (!name.equalsIgnoreCase(ENTRY_BUNDLE_CLASSPATH)) {
+				manifestSb.append(line).append(NEWLINE);
+			} else {
+				inBundleClasspathEntry = true;
+			}
+		}
+
+		// Add Bundle-ClassPath entry
+		manifestSb.append(ENTRY_BUNDLE_CLASSPATH).append(bundleClasspath)
+				.append(NEWLINE);
+
+		// Write MANIFEST.MF
+		Writer out = new OutputStreamWriter(new FileOutputStream(manifestFile),
+				"UTF-8");
+		out.write(manifestSb.toString());
+		out.flush();
+		out.close();
+	}
+
+	/**
+	 * Adapt the $äbasedir}/.classpath
+	 * @param list
+	 */
+	private void updateDotClasspath(final List<Artifact> list)
+			throws IOException, XmlPullParserException {
+		getLog().info("Update .classpath in " + project.getBasedir());
+		InputStream is = new FileInputStream(new File(project.getBasedir(),
+				".classpath"));
+		Xpp3Dom dom = Xpp3DomBuilder.build(is, "UTF-8");
+		for (Xpp3Dom cpEntry : dom.getChildren("classpathentry")) {
+			final String pathValue = cpEntry.getAttribute("path");
+			for (Artifact artifact : list) {
+				if (pathValue.indexOf(artifact.getFile().getName()) > -1) {
+					cpEntry.setAttribute("exported", "true");
+					cpEntry.setAttribute("kind", "lib");
+					cpEntry.setAttribute("path", classpathPrefix
+							+ File.separator + artifact.getFile().getName());
+				}
+			}
+		}
+		is.close();
+		Writer w;
+
+		w = new OutputStreamWriter(new FileOutputStream(new File(project
+				.getBasedir(), ".classpath")), "UTF-8");
+
+		XMLWriter writer = new PrettyPrintXMLWriter(w);
+		Xpp3DomWriter.write(writer, dom);
+		w.flush();
+		w.close();
+	}
+
+	/**
+	 * @param classpathPrefix
+	 */
+	public void setClasspathPrefix(String classpathPrefix) {
+		this.classpathPrefix = classpathPrefix;
+	}
+}

Propchange: directory/sandbox/felixk/studio-plugin/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java
------------------------------------------------------------------------------
    svn:eol-style = native