Study Maven

转载 2018年04月16日 16:00:37
1.maven的好处
①项目不需要copy jar包,项目不需要存放jar包,结果是项目极小
②使用maven开发的工程,环境统一导入别的maven项目不会报错
③方便项目升级
2.maven的两大核心
①依赖管理:对jar包的管理
②项目构建:项目在编写完成后,对项目进行编译、测试、打包、部署都可以通过命令来实现
3.安装配置(需要配置maven本地仓库路径)
4.maven的常用命令
①clean 清除maven项目下的target目录
②compile 编译项目.java -> .class
③test 单元测试 将项目下的 src/test/java目录下的单元测试类都会执行(类名结尾必须为...Test)
④package 打包 先编译->再测试->最后打包
⑤install 安装 将项目打包后安装到本地仓库
5.依赖范围(默认范围是compile)<scope></scope>
runtime例子->JDBC驱动
6.使用插件指定编译jdk版本
7.Maven依赖调节原则
①第一声明优先原则
②路径近者优先原则
8.排除依赖 -- 依赖链中删除这个依赖jar包
9.配置常量

10.maven的中央仓库(http://mvnrepository.com

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>com.hpeu</groupId>
    <artifactId>Demo1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>
    <name>helloMaven</name>
    <url>项目连接地址</url>

    <properties>
        <hadoop.version>2.7.3</hadoop.version>
        <spark.version>2.1.1</spark.version>
    </properties>

    <!-- 依赖管理,不会被运行-->
    <dependencyManagement>
        <dependencies>

        </dependencies>
    </dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <!-- hadoop-hdfs  2.7.3-->


        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <!-- hadoop-hdfs  2.7.4-->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource></resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.hpeu.demo.Demo1</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>


</project>

2pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>com.hpeu</groupId>
    <artifactId>Demo1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>
    <name>helloMaven</name>
    <url>项目连接地址</url>

    <properties>
        <hadoop.version>2.7.3</hadoop.version>
        <spark.version>2.1.1</spark.version>
    </properties>

    <!-- 依赖管理,不会被运行-->
    <dependencyManagement>
        <dependencies>

        </dependencies>
    </dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <!-- hadoop-hdfs  2.7.3-->


        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <!-- hadoop-hdfs  2.7.4-->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource></resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.hpeu.demo.Demo1</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>


</project>

转于:http://note.youdao.com/noteshare?id=a16617ff8f7d9315625373675008617c&sub=DF83F099B0AA40E4A43C40271FA020B0
收藏助手
不良信息举报
您举报文章:Study Maven
举报原因:
原因补充:

(最多只允许输入30个字)