Maven 自定义骨架

一、为什么要自定义骨架

程序员根据自己的需求,定义Maven Archetype(骨架),后续选择自定义的骨架,就可以把我们需要的pom,其他配置文件,代码的骨架,自动生成,简化开发与测试

二、如何自定义骨架

下面我们自定义一个MapReduce骨架作为实例
1.创建一个模板module
在这里插入图片描述
在这里插入图片描述

  1. 创建Java代码
  • pom.xml文件中添加
<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.5.2</version>
</dependency>
<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>2.5.2</version>
</dependency>
<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>2.5.2</version>
</dependency>
<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-mapreduce-client-core</artifactId>
      <version>2.5.2</version>
</dependency>
<dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-yarn-common</artifactId>
      <version>2.5.2</version>
</dependency>
  • MR必须运行在yarn的集群上
  • 基于Maven构建工具,简化MR在yarn集群的运行
<!--jar(main函数)   上传yarn 远程执行 bin/yarn jar xxx.jar -->
 <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <version>2.3.2</version>
         <configuration>
           <outputDirectory>${basedir}</outputDirectory>
           <archive>
             <manifest>
               <mainClass>${mainClass}</mainClass>
             </manifest>
           </archive>
         </configuration>
  </plugin>

 <extensions>
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-ssh</artifactId>
        <version>2.8</version>
      </extension>
 </extensions>

 <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>wagon-maven-plugin</artifactId>
         <version>1.0</version>
         <configuration>
           <fromFile>${project.build.finalName}.jar</fromFile>
           <url>scp://root:123456@${target-host}${target-position}</url>
           <commands>
             <command>pkill -f ${project.build.finalName}.jar</command>
             <command>nohup /opt/install/hadoop-2.5.2/bin/yarn jar ${target-position}/${project.build.finalName}.jar > /root/nohup.out 2>&amp;1 &amp;</command> 
           </commands>
           <!-- 显示运行命令的输出结果 -->
           <displayCommandOutputs>true</displayCommandOutputs>
         </configuration>
 </plugin>
  • 代码结构
/*1. Map*/
public class MyMaper extends Mapper<LongWritable, Text, Text, IntWritable>{
       
        @Override
        protected void map(LongWritable k1, Text v1, Context context) throws IOException, InterruptedException {
              //todo
                }

        }
    }

/*2. Reduce*/
public class MyReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text k2, Iterable<IntWritable> v2s, Context context) throws IOException, InterruptedException {
            //todo
        }
    }

/*3. Job*/
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "MyFirstJob");
        //作业以jar包形式  运行
        job.setJarByClass(MyMapReduce.class);

        // InputFormat
        Path path = new Path("/src/data");
        TextInputFormat.addInputPath(job,path);

        //Map
        job.setMapperClass(MyMaper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //shuffle 默认的方式处理 无需设置

        //reduce
        job.setReducerClass(MyReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //输出目录一定不能存在,由MR动态创建
        Path out = new Path("/dest2");
        FileSystem fileSystem = FileSystem.get(conf);
        fileSystem.delete(out,true);
        TextOutputFormat.setOutputPath(job,out);
        //运行job作业 
        job.waitForCompletion(true);

3.在本项目的根下:

mvn --settings D:\apache-maven-3.6.1\conf\settings.xml archetype:create-from-project

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
4.复制骨架的坐标(便于后续的安装)
在这里插入图片描述

  <groupId>com.baidu</groupId>
  <artifactId>hadoop-mr-archetype</artifactId>
	  <version>1.0-SNAPSHOT</version>

5.安装骨架

cd target\generated-sources\archetype
mvn clean install

在这里插入图片描述
6.创建并引入骨架
在这里插入图片描述
此时,创建出来的项目就会带有你所配置的pom文件中的依赖和自己构建的MapReduce的模型类。

三、如何删除idea中无用的maven骨架呢?

  • 找到 IntelliJ IDEA 的骨架配置文件
大概就是这个位置:
C:\Users${user}.IntelliJIdea${version}\system\Maven\Indices
这里面有个文件 UserArchetypes.xml,打开之后看见如下结果

在这里插入图片描述
可以删除无用的自定义骨架。若都无用,也可以删除整个文件。然后重启 IDEA , 你就会发现删除成功。

发布了24 篇原创文章 · 获赞 1 · 访问量 506

猜你喜欢

转载自blog.csdn.net/Mr_YXX/article/details/104940906