zoukankan      html  css  js  c++  java
  • maven的入门hello world

    编写pom

      pom.xml是maven的核心。Project Object Model,定义了项目的基本信息,用于描述项目如何构建,项目依赖等。

    首先,新建hello-world文件夹,新建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/maven-v4_0_0.xsd" >
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.test</groupId>
        <artifactId>hello-world</artifactId>
        <version>1.0-SNAPSHOT</version>
        <name>Maven Hello World Project</name>
    </project>

      xml头指定xml版本和编码方式,project元素是pom.xml的根元素,modelVersion指定了当前POM的版本。

      groupId定义项目属于哪个组,这个组往往和项目所在的组织或公司有关联。如果公司是mycom,项目名是myapp,groupId应该是com.mycom.myapp

      artifactId定义了当前Maven项目在组中唯一的ID,我们这个helloworld项目的定义为hello-world,你可以为不同的子模块分配artifactId,如myapp-util、myapp-domain等。

      version指定了版本

      SNAPSHOT意为”外照“,说明项目还在开发中,不稳定版本

    maven的java代码几点注意:

      1、应该把项目主代码放在src/main/java/目录下(约定)

      2、java类的包名和pom中定义的groupId和artifactId吻合。

    编写java代码:com/test/helloworld/HelloWorld.java

    package com.test.helloworld;
    
    public class HelloWorld {
        public String sayHello (){
            return "Hello Maven";
        }
    
        public static void main (String[] args){
            System.out.println( new HelloWorld().sayHello() );
        }
    }

    使用maven进行编译。

      在项目根目录运行:mvn clean compile

      项目根目录(我这里是在hello-world文件夹下)

      1、clean告诉maven清除target

      2、执行resource。resource任务(demo中没有定义项目资源,略过)

      3、compile:compile编译,讲项目主代码编译至target/classess目录

    clean和resource和compile对应一些maven插件以及插件目标

    测试。

    主代码与测试代码应该位于独立的目录中,主代码目录默认是src/main/java,测试代码目录默认是src/test/java

    测试java:hello-worldsrc estjavacom esthelloworldHelloWorldTest.java

    package com.test.helloworld;
    
    import static org.junit.Assert.assertEquals;
    import org.junit.Test;
    
    public class HelloWorldTest {
        @Test
        public void testSayHello (){
            HelloWorld helloworld=new HelloWorld();
            String result=helloworld.sayHello();
            assertEquals("Hello Maven",result);
        }
    
    }

    我们写单元测试用到了junit,所以需要在pom.xml中添加junit依赖

    <?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/maven-v4_0_0.xsd" >
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.test</groupId>
        <artifactId>hello-world</artifactId>
        <version>1.0-SNAPSHOT</version>
        <name>Maven Hello World Project</name>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.7</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>

    代码中添加了dependencies元素,元素中包含多个dependency声明项目依赖。dependency中有groupId、artifactId等坐标

    scope元素表示依赖范围,scope为test表示该依赖只对测试有效,默认值为compile,表示对主代码和测试代码有效。

    mvn clean test测试

    mvn clean package打包

    上面的Helloworld中没有指定打包类型,使用默认打包类型jar。生成一个命名规则为artifactId+version的jar文件,如果需要自定义命名可以用finalName。打包的文件存放在target中。

    默认打包的jar是不能让其他maven项目也使用的。

    mvn clean install

    将项目自动打包并安装在本地maven仓库中,让其他项目可以像junit一样下载使用。

    默认打包的jar是不包含main方法的类的。我们可以配置插件:

    <?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/maven-v4_0_0.xsd" >
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.test</groupId>
        <artifactId>hello-world</artifactId>
        <version>1.0-SNAPSHOT</version>
        <name>Maven Hello World Project</name>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.7</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
        
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>1.2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
            
        </build>
    
    
    </project>

    这样的话mvn clean install会生成两个jar,符合默认命名的是含有main类的。前缀是original的是不包含的。

    使用java命令可以执行测试。

  • 相关阅读:
    HDU 1520 Anniversary party(简单树形DP)
    HDU 4398 Template Library Management(贪心,STL)
    HDU 2829 Lawrence(斜率优化DP)
    HDU 2993 MAX Average Problem(斜率优化DP)
    HDU 3507 Print Article(斜率DP优化)
    转:操作系统各大公司笔试题汇总
    转载 ANSI、Unicode、UTF8相互转化的函数
    2011 各大IT公司笔试面试题目
    Windows Media Format SDK系统概述
    limits.h
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/6066779.html
Copyright © 2011-2022 走看看