1. 程式人生 > >maven零基礎從配置到執行helloworld(java maven helloworld)

maven零基礎從配置到執行helloworld(java maven helloworld)

首先是maven的安裝和配置

下載apache-maven-3.5.4 解壓到~/bigdata/apache-maven-3.5.4

---------------------------~/.bashrc配置------------------------------------------------------------------- export MAVEN_HOME=~/bigdata/apache-maven-3.5.4 export PATH=$MAVEN_HOME/bin:$PATH

-----------------------------------settings.xml配置jar下載的存放路徑-------------------------------------------------------------- ~/bigdata/apache-maven-3.5.4/conf/settings.xml

修改存放下載jar包的路徑:   localRepository    | The path to the local repository maven will use to store artifacts.    |    | Default: ${user.home}/.m2/repository   <localRepository>/home/appleyuchi/bigdata/apache-maven-3.5.4/jar_warehouse</localRepository>

修改下載jar軟體包的來源:  

  <mirrors>
    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>
  </mirrors>

注意,這裡的地址要寫死,如果使用~,會被當成工程當前所在路徑的。

預設下載jar包的存放地址在 \.m2\repository\org\apache\maven\plugins\maven-resources-plugin\

------------------------------maven的執行原理----------------------------------------------------------------

maven把需要下載的檔案寫入pom.xml中 mvn  compile執行時去下載這些缺少的檔案 在mvn  compile執行時打印出來的INFO log中有.pom檔案,這些是指示jar的下載位置的。

接下來是maven的使用

----------------------------------------------------------------------------------------------------------------

桌面上創立資料夾test。

首先看下路徑

(python2.7) [email protected]:~/Desktop/test$ tree . ├── pom.xml ├── src  │       └── main │                   └── java │                                └── yuchi │                                              └── HelloWorld.java

上面加粗字型的都是資料夾,其餘都是檔案

(python2.7) [email protected]:~/Desktop/test$ ls pom.xml  src  target

----------------------------------------------------------------------------------------------------------------

其中HelloWorld.java內容如下:

package yuchi;

public class HelloWorld{
    public String sayHello(){
        return "Hello World";
    }

    public static void main(String[] args){
        System.out.println(new HelloWorld().sayHello());
    }
}

這裡的package裡面要填寫src/main/java後面開始的路徑,因為只有一個資料夾yuchi

,所以形成的路徑是yuchi

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.ort/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.mycom.app</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>





<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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>yuchi.HelloWorld</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

其中,<mainClass>裡面的路徑從yuchi開始寫,寫到mainclass的名字為止

注意這裡的src/main/java相當於java系統的一個根目錄,類似於linux系統中的~

--------------------------------------------------------------------------------------------

必須在pom.xml所在的目錄下,輸入以下命令(也就是該命令對路徑是有要求的):

mvn clean install

然後會生成target資料夾

cd target

java -jar hello-world-1.0-SNAPSHOT.jar

得到執行結果: Hello World

稍微總結下:

其實執行一個helloworld是根本不用大費周章使用maven的,

這裡使用maven的目的是因為HelloWorld.java前面呼叫了一些包

操作步驟總結:

1、佈置好資料夾和檔案,形成符合規範的路徑(路徑中要有src/main/java作為根路徑)

改好pom.xml中的main.class以及程式碼檔案開頭的package

2、pom.xml路徑下執行mvn clean install

target路徑下執行java -jar hello-world-1.0-SNAPSHOT.jar

(完)

--------------------------------------------------------------------------------------------

參考文章 https://www.jianshu.com/p/10f51b4bd59e https://segmentfault.com/a/1190000013608321 https://blog.csdn.net/zhaojianting/article/details/80324533(這篇文章是用來講解命令的作用的)