SCALA+JAVA项目 几种混合编译方法

JAVA开发 ,学一下Scala 语言很快的, 一些JAVA项目部分转SCALA,尝试了几种编译方法,遇到一些问题,不能解决,记录一下。

 

个人习惯用的是Eclipse , 开发Scala 可以装个Scala-IDE插件,

最新版可以直接在Eclipse Marketplace里 Install
SCALA+JAVA项目 几种混合编译方法
 

如果选择其他版本scala , 可以在http://scala-ide.org/download/current.html  找个更新URL “Install New Software”

IDE插件中有个java to scala plugin 功能, 能方便的的JAVA 代码转成scala 代码。 

 

安装m2eclipse-scala 插件

http://scala-ide.org/docs/tutorials/m2eclipse/

 

开始转化一个maven工程的JAVA项目

首先把目录结构先按maven结构规范化, scala 文件放在src/main/java 或者src/main/scala 下面,有时会编译不了,避免不必要麻烦。

 

1. scala-maven-plugin

一开始选择用的是这个插件,后来发现用这个太坑人。

配置

dependencies

      <dependency>
			<groupId>org.springframework.scala</groupId>
			<artifactId>spring-scala_2.10</artifactId>
			<version>1.0.0.RC1</version>
		 <exclusions>
				<exclusion>
					<artifactId>scala-library</artifactId>
					<groupId>org.scala-lang</groupId>
				</exclusion>
			</exclusions> 
		</dependency>
      <dependency>
			<groupId>org.scala-lang</groupId>
			<artifactId>scala-library</artifactId>
			<version>2.10.3</version>
		</dependency>

 

plugins

<plugin>
				<groupId>net.alchim31.maven</groupId>
				<artifactId>scala-maven-plugin</artifactId>
				<version>3.2.0</version>
				<executions>
					<execution>
						<goals>
							<goal>compile</goal>
							<goal>testCompile</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>

 

看一下scala-maven对2.11还未能支持好

http://davidb.github.io/scala-maven-plugin/dependencies.html

 

不过2.10 算是能用, 到此还比较顺利, maven clean packge 就可以了。 

 

换了台机器, 问题来了, 挖掘出一堆错误,
最常见错误的是 java.lang.NoClassDefFoundError: scala/reflect/internal/Trees

Eclipse ,代码,Maven 打包复制过来,maven repo 清空, 还是报错。 

折腾了一晚上,未能解决。

 

2.Ant

参考 Scala Ant Task:http://www.scala-lang.org/old/node/98.html

 


 

	<property file="version.properties" />
	<property file="build.properties" />

	<property name="src" location="src/main/java" />
	<property name="res" location="src/main/resources" />
	<property name="build" location="build" />
	<property name="dist" location="dist" />
	<property name="web" location="src/main/webapp" />


	<target name="init_maven">
		<!--使用Maven2依赖管理 -->
		<path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.0.jar" />
		<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant" classpathref="maven-ant-tasks.classpath" />

		<!--定义Maven2变量-->
		<artifact:pom id="maven.project" file="pom.xml" />
		<artifact:dependencies pathId="maven.classpath" filesetid="maven.fileset.runtime" useScope="runtime">
			<pom refid="maven.project" />
		</artifact:dependencies>

		<taskdef resource="scala/tools/ant/antlib.xml">
			<classpath>
				<pathelement location="lib/scala-compiler.jar" />
				<pathelement location="lib/scala-library.jar" />
				<pathelement location="lib/scala-reflect.jar" />
			</classpath>
		</taskdef>
	</target>

	<target name="init">
		<!-- Create the time stamp -->
		<tstamp />
		<!-- Create the build directory structure used by compile -->
		<mkdir dir="${build}" />
		<mkdir dir="${dist}" />
	</target>

	<target name="compile" description="compile the source" depends="init_maven">
		<!-- Compile the java code from ${src} into ${build} -->
		<javac srcdir="${src}" destdir="${build}" encoding="UTF-8" nowarn="true" source="1.6" debug="true">
			<classpath refid="maven.classpath" />
		</javac>
		<scalac srcdir="${src}" destdir="${build}" >
			<classpath refid="maven.classpath" />
			<include name="**/*.scala" />
		</scalac>
	</target>

 

3. maven-ant-plugin

ant能跑,就集成在pom文件中吧

 

plugins

<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<executions>
					<execution>
						<id>scala-compile</id>
						<phase>prepare-package</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<tasks>
								<property name="build.compiler" value="extJavac" />
								<taskdef resource="scala/tools/ant/antlib.xml" >
									<classpath>
										<pathelement location="lib/scala-compiler.jar" />
										<pathelement location="lib/scala-library.jar" />
										<pathelement location="lib/scala-reflect.jar" />
									</classpath>
								</taskdef>
								<scalac srcdir="src/main/java" destdir="target/classes"
									classpathref="maven.compile.classpath">
									<include name="**/*.scala" />
								</scalac>
							</tasks>
						</configuration>
					</execution>
				</executions>
			</plugin>

 

 

 

 

 

 

你可能感兴趣的:(scala,compile)