1. 程式人生 > >用maven自定義spark任務的archetype

用maven自定義spark任務的archetype

本來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>