Eclipse下Java+Scala混合编程的Maven项目
2015-10-26 18:15
459 查看
用spark + java混合实现spark项目,我想肯定有我一样坚持要用eclipse + maven来配置开发环境,而不愿意换Intelij + sbt的同学吧。
照着文章中的步骤配置,完全ok。好文共享,原文挺好懂的,就不翻译了~
============================== 我是正文分隔线 ==============================
By Adrian Null
Maven is a build/project management tool. It favours
“convention over configuration”; it can greatly simplify builds for “standard” projects and a Maven user can usually understand the structure of another Maven project just by looking at its
Object Model). Maven is a plugin-based architecture, making it easy to add new libraries and modules to existing projects. For example, adding a new dependency usually involves only 5 extra lines in the
These “artifacts” are downloaded from repositories such as The
Central Repository.
You can also check out the official example
project which uses the same Scala plugin we will show here.
If you’re familiar with Maven, you can go ahead with the Scala
Maven Plugin.
We’ll be using the Scala Maven Plugin (GitHub
repo, website) (formerly known
as the maven-scala-plugin; renamed to honour the new naming policy where only Maven core plugins are prefixed with “maven”), by far the dominant plugin for Scala projects. Note:
the plugin includes Scala from the Central Repository so there’s no need to install it yourself if you’re compiling with Maven.
On Debian and Debian-derivatives, Maven is usually available via
OSX prior to 10.9 (Mavericks) comes with Maven 3 built in. If you don’t have it, you can get it with the package managers MacPorts, Homebrew,
or Fink. The Scala Maven Plugin requires Maven 3.0+
You can download Maven from its Apache homepage.
After extracting it (
to your directory of choice (on Linux and OSX, Unix-like systems, I
like to put them in
you need to add Maven to your environment Path variable:
Linux/OSX (option 1): Create a symlink to
Linux/OSX (option 2): Add the Maven
configuration file (e.g.
Add
whatever profile for the shell you use)
Example:
Linux/OSX (option 3): Make a
Example: you have
Put the folder you extracted in
Create a file
Add
This is probably the least intrusive way;
not, it’s a useful thing to do/have anyways. The shell script simply invokes the Maven location (which is at some other location) and passes on the arguments
Windows
Hit Start. Right click on “My Computer” and go to “Properties”
This should bring you to “Control Panel -> System and Security -> System”, giving an overview of your computer
On the left sidebar there should be four options; click on “Advanced system settings” (fourth one)
Under the “Advanced” tab, hit “Environment Variables…” in the bottom right
Note: I recommend creating/editing your User variables (top box). You can do the same with System variables though (bottom box)
Create a new variable called “MAVEN3_HOME”. Point this to your Maven folder (e.g.
backslashes to be safe, and do not include a trailing slash
Create a new variable called “MAVEN3_BIN” with this value:
Edit your Path variable: being careful not to change anything else, append
You’ll need to restart cmd to see these changes
The easiest way to create new projects is using an “archetype”.
An archetype is a general skeleton structure, or template for a project. Think back to “convention over configuration”; in our case, the Scala Maven Plugin provides an archetype for scala projects.
You run the archetype plugin like this:
[/code]
If this is your first time, you’ll notice that Maven is downloading many jar files. Maven resolves dependencies and downloads them as needed (and only once). Right now, Maven is downloading its core plugins.
Afterwards, it should give you a list of archetypes (in the hundreds). The Scala Maven Plugin was 339 on my list: “net.alchim31.maven:scala-archetype-simple (The maven-scala-plugin is used for compiling/testing/running/documenting scala code in maven.)”. You
can type “scala” (or something else) to filter the results. As of 2015 January 27, there you can choose version 3.1.4 or 3.1.5 of this plugin; you should choose the latest
[/code]
Next, Maven will ask you for a groupId, artifactId, and package. You can read the guide
to naming conventions, but in short:
groupId: inverted domain name (e.g. com.my-organization)
artifactId: project name (e.g. playground-project)
version: anything you want, but I recommend you read and follow the guidelines for Semantic
Versioning (e.g. 0.0.1)
package: the default is the groupId, but you can change this (e.g. com.my-organization)
The groupId and artifactId together should serve as a globally unique identifier for your project
When it’s done, you should see a new folder named with the artifactId.
[/code]
You’ll see Maven downloading dependencies including the Scala library (as mentioned above),JUnit, ScalaTest,
and Specs2 (the latter three are test frameworks; the archetype
includes an example “Hello world” program, and tests with each of the frameworks).
In your project root, you’ll see a
and
this archetype also includes a
Inside the
your application code, and
followed by your package structure (actually,
this with your own package and tests). If you want to mix Scala and Java source code, simply add a
inside
You can read about
page.
Example structure:
pom.xml
src
main
scala
com/my-package/… *.scala
java
com/my-package/… *.java
test
scala
com/my-package/… *.scala
java
com/my-package/… *.java
target …
Again, you can read more about the Scala Maven Plugin at its website.
By default the jar created by the Scala Maven Plugin doesn’t include a
I had to add the Maven Assembly
Plugin to my
of this plugin at the project
summary or at The
Central Repository
[/code]
After adding this,
this will also copy the Scala library into your Jar. This is normal. Be careful that your dependencies use the same version of Scala, or you will quickly end up with a massive Jar.
There are instructions at the Scala
Maven Plugin FAQs, but I thought I’d expand a bit. The maven-eclipse-plugin is
a core plugin (all plugins prefixed with “maven” are, and are available by default) to generate Eclipse configuration files. However, this plugin does not know about our new Scala source files. We’ll be using the build-helper-maven-plugin to
add new source folders:
[/code]
After adding that to your
Run
Run
variable to Eclipse
Run
Unfortunately, the integration isn’t perfect. Firstly, open up the generated
by default as it’s a dotfile, but it should be in your project root directory; where you ran
You should see something like this near the top.
[/code]
Change the
duplicate the lines and change them to
Secondly, open the
look like this. Now Eclipse knows to use the Scala editor and it won’t think that everything is a syntax error.
[/code]
Finally, in Eclipse, under “File” choose “Import…” and find your project.
m2eclipse-scala is a work in progress, and their website/repository may have updated information. It aims to ease integration between m2eclipse and Scala IDE for Eclipse.
Under “Help -> Install New Software”, enter “http://alchim31.free.fr/m2e-scala/update-site” and hit enter. You should see “Maven Integration for Eclipse -> Maven Integration for Scala IDE”.
Afer it installs, go to “New -> Project -> Other” and select “Maven Project”. Search fo “scala-archetype” choose the one with the group “net.alchim31.maven”. The wizard will more or less guide you through what was done with
To import an existing project, simply go to “File -> Import -> Existing Maven Project” and find the directory containing your project.
The first thing I do is look for “Maven” in the project page. For example, Google’s [Guava] page includes Maven
Central links. As you can see in the previous link, The Central Repository conveniently includes the snippet you have to add to your
the left sidebar.
If you can’t find Maven information at the project page, try a Google search for “[project name] maven”. Sometimes, you still won’t find anything. For scopt (Scala
command line option parser), I couldn’t find the latest version from Google. However, manually
searching The Central Repository did
Afterwards, running
[/code]
Will download any new dependencies before packaging
I’m not going to explain all of Maven in this tutorial (though I hope to add more in the future, because I do feel that the resources are a bit scattered), so here are some useful articles: - Maven
Lifecycle (explanation of goals like clean, package, install)
原文链接: http://docs.scala-lang.org/tutorials/scala-with-maven.html
照着文章中的步骤配置,完全ok。好文共享,原文挺好懂的,就不翻译了~
============================== 我是正文分隔线 ==============================
Scala with Maven
By Adrian Null
Introduction
Maven is a build/project management tool. It favours“convention over configuration”; it can greatly simplify builds for “standard” projects and a Maven user can usually understand the structure of another Maven project just by looking at its
pom.xml(Project
Object Model). Maven is a plugin-based architecture, making it easy to add new libraries and modules to existing projects. For example, adding a new dependency usually involves only 5 extra lines in the
pom.xml.
These “artifacts” are downloaded from repositories such as The
Central Repository.
You can also check out the official example
project which uses the same Scala plugin we will show here.
Jumping Ahead
If you’re familiar with Maven, you can go ahead with the ScalaMaven Plugin.
The Scala Maven Plugin
We’ll be using the Scala Maven Plugin (GitHubrepo, website) (formerly known
as the maven-scala-plugin; renamed to honour the new naming policy where only Maven core plugins are prefixed with “maven”), by far the dominant plugin for Scala projects. Note:
the plugin includes Scala from the Central Repository so there’s no need to install it yourself if you’re compiling with Maven.
Getting Maven
Linux (Debian)
On Debian and Debian-derivatives, Maven is usually available via apt-get. Just do
(sudo) apt-get install mavenand you’re good to go.
OSX
OSX prior to 10.9 (Mavericks) comes with Maven 3 built in. If you don’t have it, you can get it with the package managers MacPorts, Homebrew,or Fink. The Scala Maven Plugin requires Maven 3.0+
Manually (Red Hat Linux, OSX, Windows)
You can download Maven from its Apache homepage.After extracting it (
tar -zxvf apache-maven-X.X.X-bin.tar.gz, or use something like 7-zip)
to your directory of choice (on Linux and OSX, Unix-like systems, I
like to put them in
/opt/. On Windows I would probably put this in
C:/),
you need to add Maven to your environment Path variable:
Linux/OSX (option 1): Create a symlink to
/usr/bin, which is already on your Path
ln -s /usr/bin/mvn /opt/apache-maven-X.X.X/bin/mvn
Linux/OSX (option 2): Add the Maven
binfolder directly to your path, using your shell
configuration file (e.g.
~/.bash_profile)
Add
export PATH=$PATH:/opt/apache-maven-X.X.X/binto
.bash_profile(or
whatever profile for the shell you use)
Example:
echo "export PATH=$PATH:/opt/apache-maven-X.X.X/bin" >> ~/.bash_profile
Linux/OSX (option 3): Make a
mvnshell script in an existing path location
Example: you have
$HOME/binin your path
Put the folder you extracted in
$HOME/bin(
mv apache-maven-X.X.X "$HOME/bin/")
Create a file
mvnin
$HOME/bin
Add
"$HOME/bin/apache-maven-X.X.X/bin/mvn" $@to it, and
chmod u+x mvnto make it executable
This is probably the least intrusive way;
$HOME/binis usually added to the user’s path by default, and if
not, it’s a useful thing to do/have anyways. The shell script simply invokes the Maven location (which is at some other location) and passes on the arguments
Windows
Hit Start. Right click on “My Computer” and go to “Properties”
This should bring you to “Control Panel -> System and Security -> System”, giving an overview of your computer
On the left sidebar there should be four options; click on “Advanced system settings” (fourth one)
Under the “Advanced” tab, hit “Environment Variables…” in the bottom right
Note: I recommend creating/editing your User variables (top box). You can do the same with System variables though (bottom box)
Create a new variable called “MAVEN3_HOME”. Point this to your Maven folder (e.g.
C:\apache-maven-X.X.X). Use
backslashes to be safe, and do not include a trailing slash
Create a new variable called “MAVEN3_BIN” with this value:
%MAVEN3_HOME%\bin
Edit your Path variable: being careful not to change anything else, append
;%MAVEN3_BIN%to it
You’ll need to restart cmd to see these changes
Creating Your First Project
The easiest way to create new projects is using an “archetype”.An archetype is a general skeleton structure, or template for a project. Think back to “convention over configuration”; in our case, the Scala Maven Plugin provides an archetype for scala projects.
You run the archetype plugin like this:
mvn archetype:generate
[/code]
If this is your first time, you’ll notice that Maven is downloading many jar files. Maven resolves dependencies and downloads them as needed (and only once). Right now, Maven is downloading its core plugins.
Afterwards, it should give you a list of archetypes (in the hundreds). The Scala Maven Plugin was 339 on my list: “net.alchim31.maven:scala-archetype-simple (The maven-scala-plugin is used for compiling/testing/running/documenting scala code in maven.)”. You
can type “scala” (or something else) to filter the results. As of 2015 January 27, there you can choose version 3.1.4 or 3.1.5 of this plugin; you should choose the latest
Choose net.alchim31.maven:scala-archetype-simple version:
1: 1.4
2: 1.5
[/code]
Next, Maven will ask you for a groupId, artifactId, and package. You can read the guide
to naming conventions, but in short:
groupId: inverted domain name (e.g. com.my-organization)
artifactId: project name (e.g. playground-project)
version: anything you want, but I recommend you read and follow the guidelines for Semantic
Versioning (e.g. 0.0.1)
package: the default is the groupId, but you can change this (e.g. com.my-organization)
The groupId and artifactId together should serve as a globally unique identifier for your project
When it’s done, you should see a new folder named with the artifactId.
cdinto it and run:
mvn package
[/code]
You’ll see Maven downloading dependencies including the Scala library (as mentioned above),JUnit, ScalaTest,
and Specs2 (the latter three are test frameworks; the archetype
includes an example “Hello world” program, and tests with each of the frameworks).
Explaining this Archetype
In your project root, you’ll see a pom.xml,
srcfolder,
and
targetfolder (target folder only appears after building). Note:
this archetype also includes a
.gitignore
Inside the
srcfolder you’ll see
mainand
test;
mainincludes
your application code, and
testincludes your test suites. Inside each of those you’ll find a
scalafolder,
followed by your package structure (actually,
test/scalaincludes a sample package, but you should replace
this with your own package and tests). If you want to mix Scala and Java source code, simply add a
javafolder
inside
mainor
test.
targetincludes generated/built files, such as
.classand
.jarfiles.
You can read about
pom.xmlat the Maven
page.
Example structure:
pom.xml
src
main
scala
com/my-package/… *.scala
java
com/my-package/… *.java
test
scala
com/my-package/… *.scala
java
com/my-package/… *.java
target …
Again, you can read more about the Scala Maven Plugin at its website.
Creating a Jar
By default the jar created by the Scala Maven Plugin doesn’t include a Main-Classattribute in the manifest.
I had to add the Maven Assembly
Plugin to my
pom.xmlin order to specify custom attributes in the manifest. You can check the latest version
of this plugin at the project
summary or at The
Central Repository
<project ...>
<modelVersion>X.X.X</modelVersion>
...
<licenses>
...
</licenses>
<properties>
...
</properties>
<dependencies>
...
</dependencies>
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.your-package.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
[/code]
After adding this,
mvn packagewill also create
[artifactId]-[version]-jar-with-dependencies.jarunder
target. Note:
this will also copy the Scala library into your Jar. This is normal. Be careful that your dependencies use the same version of Scala, or you will quickly end up with a massive Jar.
Useful commands
mvn dependency:copy-dependencies: copy all libraries and dependencies to the
target/dependencyfolder
mvn clean
mvn package: compile, run tests, and create jar
Integration with Eclipse (Scala IDE)
There are instructions at the ScalaMaven Plugin FAQs, but I thought I’d expand a bit. The maven-eclipse-plugin is
a core plugin (all plugins prefixed with “maven” are, and are available by default) to generate Eclipse configuration files. However, this plugin does not know about our new Scala source files. We’ll be using the build-helper-maven-plugin to
add new source folders:
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/scala</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/scala</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
...
[/code]
After adding that to your
pom.xml:
Run
mvn eclipse:eclipse- this generates the Eclipse project files (which are already ignored by our archetype’s
.gitignore)
Run
mvn -Declipse.workspace="path/to/your/eclipse/workspace" eclipse:configure-workspace- this adds an
M2_REPOclasspath
variable to Eclipse
Run
mvn packageto ensure you have all the dependencies in your local Maven repo
Unfortunately, the integration isn’t perfect. Firstly, open up the generated
.classpathfile (it will be hidden
by default as it’s a dotfile, but it should be in your project root directory; where you ran
mvn eclipse:eclipse).
You should see something like this near the top.
<classpathentry kind="src" path="src/test/scala" output="target/test-classes" including="**/*.java"/>
<classpathentry kind="src" path="src/main/scala" including="**/*.java"/>
[/code]
Change the
*.javato
*.scala(or
duplicate the lines and change them to
*.scalaif you also have Java sources).
Secondly, open the
.projecteclipse file (again, in the same folder). Change
<buildSpec>and
<natures>to
look like this. Now Eclipse knows to use the Scala editor and it won’t think that everything is a syntax error.
<buildSpec>
<buildCommand>
<name>org.scala-ide.sdt.core.scalabuilder</name>
</buildCommand>
</buildSpec>
<natures>
<nature>org.scala-ide.sdt.core.scalanature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
[/code]
Finally, in Eclipse, under “File” choose “Import…” and find your project.
Using m2eclipse-scala for Eclipse
integration
m2eclipse-scala is a work in progress, and their website/repository may have updated information. It aims to ease integration between m2eclipse and Scala IDE for Eclipse.Under “Help -> Install New Software”, enter “http://alchim31.free.fr/m2e-scala/update-site” and hit enter. You should see “Maven Integration for Eclipse -> Maven Integration for Scala IDE”.
Afer it installs, go to “New -> Project -> Other” and select “Maven Project”. Search fo “scala-archetype” choose the one with the group “net.alchim31.maven”. The wizard will more or less guide you through what was done with
mvn archetype:generateabove, and you should end up with a new Scala project!
To import an existing project, simply go to “File -> Import -> Existing Maven Project” and find the directory containing your project.
Adding Dependencies
The first thing I do is look for “Maven” in the project page. For example, Google’s [Guava] page includes MavenCentral links. As you can see in the previous link, The Central Repository conveniently includes the snippet you have to add to your
pom.xmlon
the left sidebar.
If you can’t find Maven information at the project page, try a Google search for “[project name] maven”. Sometimes, you still won’t find anything. For scopt (Scala
command line option parser), I couldn’t find the latest version from Google. However, manually
searching The Central Repository did
Afterwards, running
mvn package
[/code]
Will download any new dependencies before packaging
Other Useful Reading
I’m not going to explain all of Maven in this tutorial (though I hope to add more in the future, because I do feel that the resources are a bit scattered), so here are some useful articles: - MavenLifecycle (explanation of goals like clean, package, install)
原文链接: http://docs.scala-lang.org/tutorials/scala-with-maven.html
相关文章推荐
- Java for循环的几种用法分析
- java得到系统时区 +0800
- Java程序设计(三)垃圾回收机制
- SSH框架的搭建(Struts2+Spring3+Hibernate3)
- java web解决表单重复提交
- 解决MyEclipse吃内存以及卡死的方法
- eclipse中查找函数的所有调用
- 精选30道Java笔试题解答
- 【JAVA核心技术卷一】Interface 接口
- JAVA WEB 导出 Word
- 【JAVA核心技术卷一】Inheritance 继承
- Java 四大域对象总结
- Java WebService入门实例
- java程序运行时会存储到什么地方
- Spring总结(二)(AOP)
- Struts 2 – 分页技术详解
- Java 流异常的标准处理(转载)
- JDK并发工具类源码学习系列——ConcurrentHashMap
- 设计模式——从单例模式开始
- Java 验证码