Re: Magic Eclipse target

Cameron Taggart <[email protected]>
Newsgroups gmane.comp.jakarta.avalon.user
Message-ID <[email protected]>
I just wrote a simple EclipseTask that will suit my needs for a couple 
of projects I'm working on.  I based it on ArtifactTask, as Steve 
suggested.  Attached is the source file.  Output from the task looks 
like this:

Eclipse .project

<?xml version="1.0"?>
<projectDescription>
  <name>ant-tasks</name>
  <comment></comment>
  <projects></projects>
  <buildSpec>
    <buildCommand>
      <name>org.eclipse.jdt.core.javabuilder</name>
      <arguments></arguments>
    </buildCommand>
  </buildSpec>
  <natures><nature>org.eclipse.jdt.core.javanature</nature></natures>
</projectDescription>

Eclipse .classpath

<?xml version="1.0"?>
<classpath>
  <classpathentry kind="con" 
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  <classpathentry kind="src" path="src/main"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="src" path="src/test" output="target/test-classes"/>
  <classpathentry kind="var" path="MAGIC_CACHE/ant/jars/ant-1.6.2.jar"/>
  <classpathentry kind="var" 
path="MAGIC_CACHE/ant/jars/ant-nodeps-1.6.2.jar"/>
  <classpathentry kind="var" 
path="MAGIC_CACHE/avalon/tools/jars/avalon-tools-magic.jar"/>
</classpath>

The only dynamic stuff right now is the project name and the jars in the 
magic cache.

-Cameron



Cameron Taggart wrote:

> David, I too am in need of an Eclipse plugin for Magic, so I'm looking 
> at the stuff Steve referenced.  I modified a ".classpath" file that 
> was produced by Maven's Eclipse plugin, so that my current project 
> that I'm building with Magic will use the jars in Magic's cache.  The 
> Magic Eclipse plugin/task will need to create a file similar to this.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <classpath>
>  <classpathentry kind="con" 
> path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
>  <classpathentry kind="src" path="src/main"/>
>  <classpathentry kind="output" path="target/classes"/>
>  <classpathentry kind="src" path="src/test" 
> output="target/test-classes"/>
>    <classpathentry kind="var" 
> path="MAGIC_CACHE/junit/jars/junit-3.8.1.jar"/>
>  <classpathentry kind="var" 
> path="MAGIC_CACHE/log4j/jars/log4j-1.2.8.jar"/>
>  <classpathentry kind="var" 
> path="MAGIC_CACHE/jaxme/jars/jaxmejs-0.2.jar"/>
>  <classpathentry kind="var" 
> path="MAGIC_CACHE/jibx/jars/jibx-run-beta3c-1.0.jar"/>
>  <classpathentry kind="var" 
> path="MAGIC_CACHE/jibx/jars/jibx-bind-beta3c-1.0.jar"/>
>  <classpathentry kind="var" 
> path="MAGIC_CACHE/jibx/jars/xpp3-beta3c-1.0.jar"/>
>  <classpathentry kind="var" path="MAGIC_CACHE/ant/jars/ant-1.6.2.jar"/>
>  <classpathentry kind="var" 
> path="MAGIC_CACHE/ant/jars/ant-nodeps-1.6.2.jar"/>
> </classpath>
>
> You can modify your ".classpath" file manually until a plugin is 
> created.  Just refresh your project when your done (no need to close 
> Eclipse).  You will also need to create a classpath variable tnamed 
> "MAGIC_CACHE" to your cache directory.  For example: "C:\Documents and 
> Settings\Owner\.magic\main".  Eclipse > Window > Preferences > Java > 
> Build Path > Classpath variables
>
> Cameron
>
> Stephen McConnell wrote:
>
>> David Leangen wrote:
>>
>>> Does there happen to be an "Eclipse" target for magic? I'm playing 
>>> around
>>> with the tutorials now and I'd like to import a project into Eclipse as
>>> easily as possible.
>>
>>
>>
>> Nope - nothing like that.
>>
>> You may want to take a look at the meta plugin as an example of 
>> creating a new plugin, and then look at the magic core tasks for 
>> ArtifactTask and BlockTask for examples of how dependency information 
>> can be used to construct xml classpath descriptors.
>>
>> Steve.
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
EclipseTask.java (text/plain, 4.9 KB)
/* 
 * Copyright 2004 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.
 */

package org.jibx.ant;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.avalon.tools.model.Context;
import org.apache.avalon.tools.model.Definition;
import org.apache.avalon.tools.model.Home;
import org.apache.avalon.tools.model.Info;
import org.apache.avalon.tools.model.Resource;
import org.apache.avalon.tools.model.ResourceRef;
import org.apache.avalon.tools.tasks.SystemTask;
import org.apache.tools.ant.BuildException;

/**
 * Create a repository plugin meta data descriptor in the form of a properties
 * file.
 * 
 * @author Cameron Taggart
 * @version $Revision: $ $Date: 2004-07-21 $
 */
public class EclipseTask extends SystemTask {

	private File classpath = new File(".classpath");

	private File project = new File(".project");

	private static final String pad = "  ";

	private static final String newline = "\n";

	public void execute() throws BuildException {
		createProject();
		createClasspath();
	}

	private void createProject() {

		Definition projectDef = getProjectDefinition();

		StringBuffer sb = new StringBuffer("<?xml version=\"1.0\"?>");
		pad(sb, 0);
		sb.append("<projectDescription>");
		pad(sb, 1);
		sb.append("<name>");
		sb.append(projectDef.getKey());
		sb.append("</name>");
		pad(sb, 1);
		sb.append("<comment></comment>");
		pad(sb, 1);
		sb.append("<projects></projects>");
		pad(sb, 1);
		sb.append("<buildSpec>");
		pad(sb, 2);
		sb.append("<buildCommand>");
		pad(sb, 3);
		sb.append("<name>org.eclipse.jdt.core.javabuilder</name>");
		pad(sb, 3);
		sb.append("<arguments></arguments>");
		pad(sb, 2);
		sb.append("</buildCommand>");
		pad(sb, 1);
		sb.append("</buildSpec>");
		pad(sb, 1);
		sb
				.append("<natures><nature>org.eclipse.jdt.core.javanature</nature></natures>");
		pad(sb, 0);
		sb.append("</projectDescription>");

		System.out.println(sb);

		write(project, sb.toString().getBytes());
	}

	private void createClasspath() {
		StringBuffer sb = new StringBuffer("<?xml version=\"1.0\"?>");
		pad(sb, 0);
		sb.append("<classpath>");
		pad(sb, 1);
		sb
				.append("<classpathentry kind=\"con\" path=\"org.eclipse.jdt.launching.JRE_CONTAINER\"/>");
		pad(sb, 1);
		sb.append("<classpathentry kind=\"src\" path=\"src/main\"/>");
		pad(sb, 1);
		sb.append("<classpathentry kind=\"output\" path=\"target/classes\"/>");
		pad(sb, 1);
		sb
				.append("<classpathentry kind=\"src\" path=\"src/test\" output=\"target/test-classes\"/>");

		Definition projectDef = getProjectDefinition();
		ResourceRef[] rrs = projectDef.getResourceRefs();
		Resource[] rs = getResources(rrs);

		for (int i = 0; i < rs.length; i++) {
			pad(sb, 1);
			sb.append(getEclipseClasspath(rs[i]));
		}

		pad(sb, 0);
		sb.append("</classpath>");

		System.out.println(sb);

		write(classpath, sb.toString().getBytes());
	}

	private void pad(StringBuffer sb, int n) {
		sb.append(newline);
		for (int i = 0; i < n; i++) {
			sb.append(pad);
		}
	}

	private void write(File file, byte[] ba) {

		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			fos.write(ba);
		} catch (FileNotFoundException e) {
			throw new RuntimeException("unable to write file: " + file, e);
		} catch (IOException e) {
			throw new RuntimeException("unable to write file: " + file, e);
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e1) {
					// ignore
				}
			}
		}

	}

	private Definition getProjectDefinition() {
		Context ctx = getContext();
		String key = ctx.getKey();
		ResourceRef ref = new ResourceRef(key);
		Home home = getHome();
		return home.getDefinition(ref);
	}

	private Resource[] getResources(ResourceRef[] rrs) {
		Resource[] rs = new Resource[rrs.length];
		for (int i = 0; i < rrs.length; i++) {
			rs[i] = getHome().getResource(rrs[i]);
		}
		return rs;
	}

	// <classpathentry kind="var" path="MAGIC_CACHE/ant/jars/ant-1.6.2.jar"/>
	private String getEclipseClasspath(Resource r) {
		Info info = r.getInfo();
		StringBuffer sb = new StringBuffer();
		sb.append("<classpathentry kind=\"var\" path=\"MAGIC_CACHE/");
		sb.append(info.getGroup());
		sb.append("/jars/");
		sb.append(info.getName());
		if (info.getVersion() != null) {
			sb.append("-");
			sb.append(info.getVersion());
		}
		sb.append(".");
		sb.append(info.getType());
		sb.append("\"/>");
		return sb.toString();
	}

}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.