add a basic C# plugin using the Ant tasks

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@231226 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-08-10 13:12:25 +00:00
parent ad947569c8
commit 53865c867f
7 changed files with 753 additions and 0 deletions

View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-parent</artifactId>
<version>2.0-beta-1-SNAPSHOT</version>
</parent>
<artifactId>maven-csharp-plugin</artifactId>
<packaging>maven-plugin</packaging>
<name>csharp-plugin</name>
<version>1.0-SNAPSHOT</version>
<inceptionYear>2005</inceptionYear>
<description>M2 csharp plugin</description>
<contributors>
<contributor>
<name>Gilles Dodinet</name>
<email>gdodinet@karmicsoft.com</email>
<organization>Karmic Software Research</organization>
</contributor>
</contributors>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.0-beta-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0-beta-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>plexus</groupId>
<artifactId>plexus-compiler-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.6.5</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,157 @@
package org.apache.plugins.csharp.compiler;
/*
* Copyright 2005 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 org.apache.plugins.csharp.helpers.AntBuildListener;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.optional.dotnet.CSharp;
import org.codehaus.plexus.compiler.AbstractCompiler;
import org.codehaus.plexus.compiler.CompilerConfiguration;
import java.io.File;
import java.util.List;
import java.util.Map;
/**
* @author <a href="mailto:gdodinet@karmicsoft.com">Gilles Dodinet</a>
* @version $Id$
*/
public class CSharpCompiler
extends AbstractCompiler
{
private File basedir;
private AntBuildListener antBuildListener;
private String references;
public List compile( CompilerConfiguration config )
throws Exception
{
CSharp csc = createCompiler( config );
csc.execute();
return null;
}
private CSharp createCompiler( CompilerConfiguration config )
throws Exception
{
Map compilerOptions = config.getCompilerOptions();
CSharp csc = new CSharp();
Project antProject = new Project();
antProject.setBaseDir( basedir );
if ( antBuildListener == null )
{
antBuildListener = new AntBuildListener();
}
antProject.addBuildListener( antBuildListener );
csc.setProject( antProject );
csc.setOptimize( getBooleanOption( compilerOptions, "optimize", true ) );
csc.setUnsafe( getBooleanOption( compilerOptions, "unsafe", false ) );
csc.setIncremental( getBooleanOption( compilerOptions, "incremental", false ) );
csc.setFullPaths( getBooleanOption( compilerOptions, "fullpaths", true ) );
csc.setWarnLevel( getIntOption( compilerOptions, "warnLevel", 4 ) );
csc.setDebug( getBooleanOption( compilerOptions, "debug", true ) );
csc.setMainClass( (String) compilerOptions.get( "mainClass" ) );
String type = (String) compilerOptions.get( "type" );
csc.setTargetType( type );
csc.setReferences( references );
File destDir = new File( config.getOutputLocation() );
if ( !destDir.exists() )
{
destDir.mkdirs();
}
csc.setDestDir( destDir );
String destFileName = (String) compilerOptions.get( "destFile" );
if ( destFileName == null )
{
destFileName = (String) compilerOptions.get( "mainClass" );
}
csc.setDestFile( new File( destDir, destFileName + "." + getTypeExtension( type ) ) );
return csc;
}
/**
* @param type
* @return
* @throws Exception
*/
private String getTypeExtension( String type )
throws Exception
{
if ( "exe".equals( type ) || "winexe".equals( type ) )
{
return "exe";
}
if ( "library".equals( type ) || "module".equals( type ) )
{
return "dll";
}
throw new Exception( "Unrecognized type" );
}
private boolean getBooleanOption( Map options, String optionName, boolean defaultValue )
{
Boolean optionValue = (Boolean) options.get( optionName );
return optionValue != null ? optionValue.booleanValue() : defaultValue;
}
private int getIntOption( Map options, String optionName, int defaultValue )
{
Integer optionValue = (Integer) options.get( optionName );
return optionValue != null ? optionValue.intValue() : defaultValue;
}
/**
* @param basedir
*/
public void setBasedir( File basedir )
{
this.basedir = basedir;
}
/**
* @param antBuildListener The antBuildListener to set.
*/
public void setAntBuildListener( AntBuildListener antBuildListener )
{
this.antBuildListener = antBuildListener;
}
/**
* @param additionalModules The additionalModules to set.
*/
public void setReferences( String additionalModules )
{
this.references = additionalModules;
}
}

View File

@ -0,0 +1,365 @@
package org.apache.plugins.csharp.compiler;
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.apache.plugins.csharp.helpers.AntBuildListener;
import org.codehaus.plexus.compiler.CompilerConfiguration;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
* Copyright 2005 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.
*/
/**
* @author <a href="mailto:gdodinet@karmicsoft.com">Gilles Dodinet</a>
* @version $Id$
* @description Compiles c# sources
* @goal compile
* @phase compile
* @requiresDependencyResolution compile
*/
public class CSharpCompilerMojo
extends AbstractMojo
{
/**
* @parameter
*/
private String debug = Boolean.TRUE.toString();
/**
* @parameter
*/
private String optimize = Boolean.TRUE.toString();
/**
* @parameter
*/
private String unsafe = Boolean.FALSE.toString();
/**
* @parameter
*/
private String incremental = Boolean.FALSE.toString();
/**
* @parameter
*/
private String fullPaths = Boolean.TRUE.toString();
/**
* @parameter
*/
private String warnLevel = "4";
/**
* @parameter
*/
private String mainClass;
/**
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
/**
* @parameter expression="${project.build.outputDirectory}"
* @required
* @readonly
*/
private String outputDirectory;
/**
* @parameter
*/
private String destFile;
/**
* @parameter
* @required
*/
private String type = "library";
/**
* @parameter expression="${project.compileSourceRoots}"
* @required
* @readonly
*/
private List compileSourceRoots;
public void execute()
throws MojoExecutionException
{
CompilerConfiguration config = new CompilerConfiguration();
Map compilerOptions = new HashMap();
config.setOutputLocation( outputDirectory );
config.setSourceLocations( compileSourceRoots );
compilerOptions.put( "optimize", Boolean.valueOf( optimize ) );
compilerOptions.put( "unsafe", Boolean.valueOf( unsafe ) );
compilerOptions.put( "incremental", Boolean.valueOf( incremental ) );
compilerOptions.put( "fullPaths", Boolean.valueOf( fullPaths ) );
compilerOptions.put( "warnLevel", Integer.valueOf( warnLevel ) );
compilerOptions.put( "mainClass", mainClass );
compilerOptions.put( "destFile", destFile );
//until handlers ready
compilerOptions.put( "type", type );
//compilerOptions.put("type", project.getPackaging());
config.setCompilerOptions( compilerOptions );
CSharpCompiler compiler = new CSharpCompiler();
compiler.setBasedir( project.getBasedir() );
compiler.setAntBuildListener( new AntBuildListener( this.getLog() ) );
String artifactList = "";
Set artifacts = project.getArtifacts();
int u = 0;
for ( Iterator i = artifacts.iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
if ( "dll".equals( artifact.getType() ) )
{
File file = artifact.getFile();
artifactList += file.getAbsolutePath();
if ( u < artifacts.size() - 1 )
{
artifactList += ":";
}
}
u++;
}
compiler.setReferences( artifactList );
try
{
compiler.compile( config );
}
catch ( Exception e )
{
throw new MojoExecutionException( "Error compiling C# sources", e );
}
}
/**
* @return Returns the debug.
*/
public String getDebug()
{
return debug;
}
/**
* @param debug The debug to set.
*/
public void setDebug( String debug )
{
this.debug = debug;
}
/**
* @return Returns the fullPaths.
*/
public String getFullPaths()
{
return fullPaths;
}
/**
* @param fullPaths The fullPaths to set.
*/
public void setFullPaths( String fullPaths )
{
this.fullPaths = fullPaths;
}
/**
* @return Returns the incremental.
*/
public String getIncremental()
{
return incremental;
}
/**
* @param incremental The incremental to set.
*/
public void setIncremental( String incremental )
{
this.incremental = incremental;
}
/**
* @return Returns the mainClass.
*/
public String getMainClass()
{
return mainClass;
}
/**
* @param mainClass The mainClass to set.
*/
public void setMainClass( String mainClass )
{
this.mainClass = mainClass;
}
/**
* @return Returns the optimize.
*/
public String getOptimize()
{
return optimize;
}
/**
* @param optimize The optimize to set.
*/
public void setOptimize( String optimize )
{
this.optimize = optimize;
}
/**
* @return Returns the outputDirectory.
*/
public String getOutputDirectory()
{
return outputDirectory;
}
/**
* @param outputDirectory The outputDirectory to set.
*/
public void setOutputDirectory( String outputDirectory )
{
this.outputDirectory = outputDirectory;
}
/**
* @return Returns the compileSourceRoots.
*/
public List getCompileSourceRoots()
{
return compileSourceRoots;
}
/**
* @param compileSourceRoots The compileSourceRoots to set.
*/
public void setCompileSourceRoots( List compileSourceRoots )
{
this.compileSourceRoots = compileSourceRoots;
}
/**
* @return Returns the project.
*/
public MavenProject getProject()
{
return project;
}
/**
* @param project The project to set.
*/
public void setProject( MavenProject project )
{
this.project = project;
}
/**
* @return Returns the unsafe.
*/
public String getUnsafe()
{
return unsafe;
}
/**
* @param unsafe The unsafe to set.
*/
public void setUnsafe( String unsafe )
{
this.unsafe = unsafe;
}
/**
* @return Returns the warnLevel.
*/
public String getWarnLevel()
{
return warnLevel;
}
/**
* @param warnLevel The warnLevel to set.
*/
public void setWarnLevel( String warnLevel )
{
this.warnLevel = warnLevel;
}
/**
* @return Returns the destFile.
*/
public String getDestFile()
{
return destFile;
}
/**
* @param destFile The destFile to set.
*/
public void setDestFile( String destFile )
{
this.destFile = destFile;
}
/**
* @return Returns the type.
*/
public String getType()
{
return type;
}
/**
* @param type The type to set.
*/
public void setType( String type )
{
this.type = type;
}
}

View File

@ -0,0 +1,92 @@
package org.apache.plugins.csharp.helpers;
/*
* Copyright 2005 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 org.apache.maven.plugin.logging.Log;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.Project;
/**
* @author <a href="mailto:gdodinet@karmicsoft.com">Gilles Dodinet</a>
* @version $Id$
*/
public final class AntBuildListener
implements BuildListener
{
private Log log;
public AntBuildListener()
{
}
public AntBuildListener( Log log )
{
this.log = log;
}
public void buildFinished( BuildEvent arg0 )
{
}
public void buildStarted( BuildEvent arg0 )
{
}
public void targetStarted( BuildEvent arg0 )
{
}
public void targetFinished( BuildEvent arg0 )
{
}
public void taskStarted( BuildEvent arg0 )
{
}
public void taskFinished( BuildEvent arg0 )
{
}
public void messageLogged( BuildEvent e )
{
if ( log == null )
{
System.out.println( e.getMessage() );
return;
}
switch ( e.getPriority() )
{
case Project.MSG_DEBUG:
log.debug( e.getMessage() );
break;
case Project.MSG_INFO :
log.info( e.getMessage() );
break;
case Project.MSG_WARN :
log.warn( e.getMessage() );
break;
case Project.MSG_ERR :
log.error( e.getMessage() );
break;
default :
break;
}
}
}

View File

@ -0,0 +1,34 @@
<component-set>
<components>
<component>
<role>org.apache.maven.artifact.handler.ArtifactHandler</role>
<role-hint>dll</role-hint>
<implementation>org.apache.maven.artifact.handler.DefaultArtifactHandler</implementation>
<configuration>
<type>dll</type>
<extension>dll</extension>
</configuration>
</component>
<component>
<role>org.apache.maven.artifact.handler.ArtifactHandler</role>
<role-hint>exe</role-hint>
<implementation>org.apache.maven.artifact.handler.DefaultArtifactHandler</implementation>
<configuration>
<type>exe</type>
<extension>exe</extension>
</configuration>
</component>
<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<role-hint>dotnetexe</role-hint>
<implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
<configuration>
<phases>
<compile>csharp:compile</compile>
<install>install:install</install>
<deploy>deploy:deploy</deploy>
</phases>
</configuration>
</component>
</components>
</component-set>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-csharp-plugin-test</artifactId>
<packaging>dotnetexe</packaging>
<name>csharp-plugin-test</name>
<version>1.0-SNAPSHOT</version>
<inceptionYear>2005</inceptionYear>
<description>M2 csharp plugin Test</description>
<contributors>
<contributor>
<name>Gilles Dodinet</name>
<email>gdodinet@karmicsoft.com</email>
<organization>Karmic Software Research</organization>
</contributor>
</contributors>
<build>
<sourceDirectory>src/main/csharp</sourceDirectory>
<plugins>
<plugin>
<extensions>true</extensions>
<artifactId>maven-csharp-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<mainClass>Com.Test.Test2</mainClass>
<destFile>Test2</destFile>
<type>exe</type>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,17 @@
using System;
using System.Text;
namespace Com.Test {
public class Test2 {
public int MyProp {
get { return 1; }
}
[STAThread]
public static void Main(string[] args) {
System.Console.WriteLine("HELLO WORLD");
}
}
}