首先安装Maven,Maven的安装很简单,这里就不在说了。
先要确定把工程放在哪个路径下,创建一个文件夹并且在该文件夹下打开shell命令。可以先运行下面的命令,创建一个工程:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
初次运行该命令会有些慢,因为maven需要下载一些最经常用到的artifacts到本地仓库(repository)。由于网络等原因,用户可能需要多次运行该命令,直到成功。
该命令会创建一个文件夹,名字就是所给的artifactid。文件夹结构如下,这是maven约定的:
工程的源代码放在src/main/java路径下,测试源代码放在src/test/java路径下。
pom.xml是整个工程配置的核心,样式如下:
You executed the Maven goal archetype:generate, and passed in various parameters to that goal. The prefix archetype is the plugin that contains the goal. If you are familiar with Ant, you may conceive of this as similar to a task. This goal created a simple project based upon an archetype. Suffice it to say for now that a plugin is a collection of goals with a general common purpose. For example the jboss-maven-plugin, whose purpose is "deal with various jboss items".
build工程:
mvn package
Unlike the first command executed (archetype:generate) you may notice the second is simply a single word - package. Rather than a goal, this is a phase. A phase is a step in the build lifecycle, which is an ordered sequence of phases. When a phase is given, Maven will execute every phase in the sequence up to and including the one defined. For example, if we execute the compile phase, the phases that actually get executed are:
validate
generate-sources process-sources generate-resources process-resources compile用户可以通过下面的命令运行程序:
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Maven的Phases:
- validate: validate the project is correct and all necessary information is available
- compile: compile the source code of the project
- test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
- package: take the compiled code and package it in its distributable format, such as a JAR.
- integration-test: process and deploy the package if necessary into an environment where integration tests can be run
- verify: run any checks to verify the package is valid and meets quality criteria
- install: install the package into the local repository, for use as a dependency in other projects locally
- deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
- clean: cleans up artifacts created by prior builds
- site: generates site documentation for this project
例如:
mvn clean dependency:copy-dependencies package
这个命令会清空工程、复制依赖并且build工程。
Spark的Java部分需要Maven来管理,首先要安装两个插件:
org.apache.maven.plugins maven-compiler-plugin 3.1 maven-assembly-plugin 2.4.1 jar-with-dependencies make-assembly package single
先说第一个插件maven-compiler-plugin。Maven默认的Jdk compiler是1.5,需要通过该插件改变编译器,否则一些新的特性,例如lamda表达式不能使用。
第二插件负责将整个工程、包括依赖的库打包,这样就可以通过spark-submit来在集群上运行了。