用maven自定义spark任务的archetype

来源:互联网 发布:昌硕ciq软件下载 编辑:程序博客网 时间:2024/06/10 23:56
本来spark工程是推荐用sbt的,但是sbt因为某些原因在国内实在是太慢了,所以尝试用eclipse+maven+scala插件,
然而scala-tools提供的archetype默认使用scala2.7.0,让人每次都去修改,然后又要去添加Hadoop、spark之类的依赖,有时候还出现包冲突
,有时候一不小心忘了,编译的时候还会出错。   于是我决定生成一个自己用的archetype,以图省事。


以下流程需要使用自己安装的maven,使用eclipse自带的maven会报错。
可以从Window->Preferences->Maven->Installations里面add自己maven的安装目录。

1、创建一个maven工程,最好用scala的archetype,因为在上面修改起来方便,随便取个名字,比如spark-archetype
2、把自己想要的目录下面随便建个文件,这一点是为了让得到的archetype会自动生成文件夹,如果有不用建个文件就保留文件夹的
方法,请务必联系我


3、编辑pom.xml,添加常用的依赖,比如spark、Hadoop、hbase,等等。  可以把version用变量的方式提到前面,方便修改。像这样:

</pre><pre name="code" class="html"> <properties>    <scala.version>2.10.4</scala.version>    <spark.version>1.4.1</spark.version>    <hadoop.version>2.2.0</hadoop.version>  </properties>
然后在build里面添加archetype插件:
<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-archetype-plugin</artifactId>    <version>2.2</version></plugin>
完整的pom.xml文件会放在文后。

4、右键该pom.xml文件,run as->maven build...  输入archetype:create-from-project,完成后,target下面会生成generated_sources文件夹,下面会有一个
名字叫archetype的工程。
此步骤也可以在cmd中cd到工程目录下,使用 mvn archetype:create-from-project命令

5、右键4中生成的archetype工程下面的pom.xml文件,即target/generated_sources/archetype/pom.xml文件,run as->maven build...   输入install,该archetype
就会安装到本地的maven库中,也可以在cmd中cd到archetype目录下面,使用mvn install命令
然后通过file->new ->other...->Maven Project,后面选择archetype的时候,catalog选择Default Local,会出现刚才安装的archetype:



附录,完整的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.tcl</groupId>  <artifactId>spark-archetype</artifactId>  <version>0.0.3</version>  <inceptionYear>2008</inceptionYear>  <properties>    <scala.version>2.10.4</scala.version>    <spark.version>1.4.1</spark.version>    <hadoop.version>2.2.0</hadoop.version>  </properties>   <repositories>    <repository>      <id>scala-tools.org</id>      <name>Scala-Tools Maven2 Repository</name>      <url>http://scala-tools.org/repo-releases</url>    </repository>  </repositories>   <pluginRepositories>    <pluginRepository>      <id>scala-tools.org</id>      <name>Scala-Tools Maven2 Repository</name>      <url>http://scala-tools.org/repo-releases</url>    </pluginRepository>  </pluginRepositories>   <dependencies>    <dependency>      <groupId>org.scala-lang</groupId>      <artifactId>scala-library</artifactId>      <version>${scala.version}</version>    </dependency>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.4</version>      <scope>test</scope>    </dependency>    <dependency>      <groupId>org.specs</groupId>      <artifactId>specs</artifactId>      <version>1.2.5</version>      <scope>test</scope>    </dependency>    <dependency>    <groupId>org.apache.spark</groupId><span style="white-space:pre"></span><artifactId>spark-core_2.10</artifactId><span style="white-space:pre"></span><version>${spark.version}</version><span style="white-space:pre"></span><exclusions><span style="white-space:pre"></span><exclusion><span style="white-space:pre"></span><artifactId>hadoop-client</artifactId><span style="white-space:pre"></span><groupId>org.apache.hadoop</groupId><span style="white-space:pre"></span></exclusion><span style="white-space:pre"></span><exclusion><span style="white-space:pre"></span><artifactId>hadoop-core</artifactId><span style="white-space:pre"></span><groupId>org.apache.hadoop</groupId><span style="white-space:pre"></span></exclusion><span style="white-space:pre"></span><exclusion><span style="white-space:pre"></span><artifactId>guava</artifactId><span style="white-space:pre"></span><groupId>com.google.guava</groupId><span style="white-space:pre"></span></exclusion><span style="white-space:pre"></span></exclusions>  </dependency>  <dependency><span style="white-space:pre"></span><groupId>org.apache.spark</groupId><span style="white-space:pre"></span><artifactId>spark-sql_2.10</artifactId><span style="white-space:pre"></span><version>${spark.version}</version>  </dependency>  <dependency><span style="white-space:pre"></span><groupId>org.apache.spark</groupId><span style="white-space:pre"></span><artifactId>spark-hive_2.10</artifactId><span style="white-space:pre"></span><version>${spark.version}</version>  </dependency>  <dependency><span style="white-space:pre"></span><groupId>org.apache.hadoop</groupId><span style="white-space:pre"></span><artifactId>hadoop-client</artifactId><span style="white-space:pre"></span><version>${hadoop.version}</version><span style="white-space:pre"></span><exclusions><span style="white-space:pre"></span><exclusion><span style="white-space:pre"></span><artifactId>guava</artifactId><span style="white-space:pre"></span><groupId>com.google.guava</groupId><span style="white-space:pre"></span></exclusion><span style="white-space:pre"></span></exclusions>  </dependency>  <dependency><span style="white-space:pre"></span><groupId>org.apache.hadoop</groupId><span style="white-space:pre"></span><artifactId>hadoop-common</artifactId><span style="white-space:pre"></span><version>${hadoop.version}</version>  <exclusions><span style="white-space:pre"></span>  <exclusion><span style="white-space:pre"></span><artifactId>guava</artifactId><span style="white-space:pre"></span><groupId>com.google.guava</groupId><span style="white-space:pre"></span></exclusion>  </exclusions>  </dependency>  </dependencies>   <build>    <sourceDirectory>src/main/scala</sourceDirectory>    <testSourceDirectory>src/test/scala</testSourceDirectory>    <plugins>      <plugin>        <groupId>org.scala-tools</groupId>        <artifactId>maven-scala-plugin</artifactId>        <executions>          <execution>            <goals>              <goal>compile</goal>              <goal>testCompile</goal>            </goals>          </execution>        </executions>        <configuration>          <scalaVersion>${scala.version}</scalaVersion>          <args>            <arg>-target:jvm-1.7</arg>          </args>        </configuration>      </plugin>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-eclipse-plugin</artifactId>        <configuration>          <downloadSources>true</downloadSources>          <buildcommands>            <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>          </buildcommands>          <additionalProjectnatures>            <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>          </additionalProjectnatures>          <classpathContainers>            <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>            <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>          </classpathContainers>        </configuration>      </plugin>      <plugin>            <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-archetype-plugin</artifactId>            <version>2.2</version>        </plugin>    </plugins>  </build>  <reporting>    <plugins>      <plugin>        <groupId>org.scala-tools</groupId>        <artifactId>maven-scala-plugin</artifactId>        <configuration>          <scalaVersion>${scala.version}</scalaVersion>        </configuration>      </plugin>    </plugins>  </reporting></project>



1 0