Maven 初体验

  1. maven 是什么?

    maven 是一个项目构建工具,通过一个pom.xml 文件管理我们java工程所依赖的类,而不需要我们自己手动添加依赖简化了我们对依赖jar 包的导入;

  2. maven 安装:

    下载地址:http://maven.apache.org/download.cgi

    相关站点:http://mvnrepository.com/

    解压到目录:E:\work-space\eclipse3.7\lsjiankong\maven3.3\  下

    配置eclipse:  (eclipse 需要有maven 插件)

    Window -> preferences -> Maven -> Installations - Add

    Maven 初体验_第1张图片

    配置环境变量 M2_HOME:

    测试安装是否成功:执行 mvn -version 命令

  3. Maven 初体验_第2张图片
  4. 配置maven3.3\apache-maven-3.3.3\conf\settings.xml


<!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <!-- 这个路径是配置存储jar资源的目录-->
  <localRepository>E:\work-space\eclipse3.7\local_mavenResource</localRepository>
<!-- 这个路径是配置如果本地jar资源目录没有要使用的jar包就会去中心资源库下载,为了减少占用公共资源,
公司一般都会建立一个本地中心资源库,这里配置的路径就是公司的中心资源库-->
<mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>LSPT Mirror</name>
      <url>http://host:8081/nexus/content/groups/public</url>
</mirror>
<!-- 连接中心资源库认证所需要的用户名和密码-->
<server>

     <id>nexus-releases</id>

     <username>username</username>

     <password>password</password>

   </server>

    <server>

     <id>nexus-snapshots</id>

     <username>username</username>

     <password>password</password>

   </server>

4.maven 工程中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/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <parent>
    <artifactId>flume-parent</artifactId>
    <groupId>org.apache.flume</groupId>
    <version>1.6.0</version>
  </parent>

  <groupId>org.apache.flume</groupId>
  <artifactId>flume-ng-core</artifactId>
  <name>Flume NG Core</name>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.apache.avro</groupId>
                    <artifactId>avro-maven-plugin</artifactId>
                    <versionRange>[${avro.version},)</versionRange>
                    <goals>
                      <goal>idl-protocol</goal>
                      <goal>schema</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <execute />
                  </action>
                </pluginExecution>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>com.thoughtworks.paranamer</groupId>
                    <artifactId>paranamer-maven-plugin</artifactId>
                    <versionRange>[2.3,)</versionRange>
                    <goals>
                      <goal>generate</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <versionRange>[1.7,)</versionRange>
                    <goals>
                      <goal>run</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>

      <plugin>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro-maven-plugin</artifactId>
        <executions>
          <execution>
            <phase>generate-sources</phase>
            <goals>
              <goal>schema</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>com.thoughtworks.paranamer</groupId>
        <artifactId>paranamer-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>run</id>
            <configuration>
              <sourceDirectory>${project.build.directory}/generated-sources/avro</sourceDirectory>
              <outputDirectory>${project.build.directory}/classes</outputDirectory>
            </configuration>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.rat</groupId>
        <artifactId>apache-rat-plugin</artifactId>
      </plugin>

    </plugins>
  </build>

  <profiles>
    <profile>
      <id>not-windows</id>
      <activation>
        <os>
          <family>!Windows</family>
        </os>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
              <execution>
                <id>generate-version</id>
                <phase>generate-sources</phase>
                <configuration>
                  <target>
                    <mkdir dir="${project.build.directory}/generated-sources/java"/>
                    <exec executable="sh">
                      <arg
                        line = "${basedir}/scripts/saveVersion.sh ${project.version} ${project.build.directory}" />
                    </exec>
                  </target>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.7</version>
            <executions>
              <execution>
                <id>add-source</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>add-source</goal>
                </goals>
                <configuration>
                  <sources>
                    <source>target/generated-sources/java</source>
                  </sources>
                </configuration>
              </execution>
            </executions>
          </plugin>

        </plugins>
      </build>
    </profile>

    <profile>
      <id>windows</id>
      <activation>
        <os>
          <family>Windows</family>
        </os>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
              <execution>
                <id>generate-version</id>
                <phase>generate-sources</phase>
                <configuration>
                  <target>
                    <mkdir dir="${project.build.directory}/generated-sources/java/org/apache/flume"/>
                    <exec executable="powershell">
                      <arg
                              line = "-executionpolicy unrestricted -file ${basedir}\scripts\saveVersion.ps1  ${project.version} ${project.build.directory}" />
                    </exec>
                  </target>
                </configuration>
                <goals>
                  <goal>run</goal>
                </goals>
              </execution>
            </executions>
          </plugin>

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.7</version>
            <executions>
              <execution>
                <id>add-source</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>add-source</goal>
                </goals>
                <configuration>
                  <sources>
                    <source>target/generated-sources/java</source>
                  </sources>
                </configuration>
              </execution>
            </executions>
          </plugin>

        </plugins>
      </build>
    </profile>

  </profiles>


  <dependencies>

    <dependency>
      <groupId>org.apache.flume</groupId>
      <artifactId>flume-ng-sdk</artifactId>
    </dependency>


  </dependencies>

</project>


你可能感兴趣的:(maven,pom)