1. 程式人生 > >Maven建立java專案

Maven建立java專案

1.使用Maven模板建立一個Java專案,在cmd命令中輸入如下:
mvn archetype:generate -DgroupId={project-packaging} 
-DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
C:\software\develop\workplace>mvn archetype:generate -DgroupId=com.test -Dartifa
ctId=testMaven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMod
e=false
[INFO] Scanning for
projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate
-source s @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-source s @ standalone-pom <<< [INFO] [INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Batch mode [INFO] -------------------------------------------------------------------------
--- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0 [INFO] ------------------------------------------------------------------------- --- [INFO] Parameter: basedir, Value: C:\software\develop\workplace [INFO] Parameter: package, Value: com.test [INFO] Parameter: groupId, Value: com.test [INFO] Parameter: artifactId, Value: testMaven [INFO] Parameter: packageName, Value: com.test [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: C:\software\develop\work place\testMaven [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 15.483 s [INFO] Finished at: 2018-09-12T11:56:04+08:00 [INFO] Final Memory: 14M/89M [INFO] ------------------------------------------------------------------------
2.為了使生成的專案變成Eclipse專案,在終端命令列中輸入如下命令:
mvn eclipse:eclipse
C:\software\develop\workplace\testMaven>mvn eclipse:eclipse
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testMaven 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-eclipse-plugin:2.10:eclipse (default-cli) > generate-resources
@ testMaven >>>
[INFO]
[INFO] <<< maven-eclipse-plugin:2.10:eclipse (default-cli) < generate-resources
@ testMaven <<<
[INFO]
[INFO] --- maven-eclipse-plugin:2.10:eclipse (default-cli) @ testMaven ---
[INFO] Using Eclipse Workspace: C:\software\develop\workplace
[WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C
:\Program Files\Java1.6\jre6
[WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C
:\Program Files\Java\jdk1.7.0_45\jre
[WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C
:\Program Files\java8\jre8
[INFO] no substring wtp server match.
[INFO] Using as WTP server : Apache Tomcat v7.0
[INFO] Adding default classpath container: org.eclipse.jdt.launching.JRE_CONTAIN
ER
[INFO] Not writing settings - defaults suffice
[INFO] File C:\software\develop\workplace\testMaven\.project already exists.
       Additional settings will be preserved, run mvn eclipse:clean if you want
old settings to be removed.
[INFO] Wrote Eclipse project for "testMaven" to C:\software\develop\workplace\te
stMaven.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.898 s
[INFO] Finished at: 2018-09-12T11:58:07+08:00
[INFO] Final Memory: 12M/184M
[INFO] ------------------------------------------------------------------------
3.上面的命令列中輸出的日誌有警告資訊:
[INFO] Using Eclipse Workspace: C:\software\develop\workplace
[WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C
:\Program Files\Java1.6\jre6
[WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C
:\Program Files\Java\jdk1.7.0_45\jre
[WARNING] Workspace defines a VM that does not contain a valid jre/lib/rt.jar: C
:\Program Files\java8\jre8

首先是因為我的電腦上安裝了三個版本的JDK,所以才出現三條警告;出現警告的原因是環境變數配置中JRE目錄的問題,通常情況下我們是配置到JRE這一層目錄的,如:

C:\Program Files\Java\jdk1.7.0_45\jre

實際上只需要將目錄做個調整,這個警告的問題就可以解決掉了:

C:\Program Files\Java\jdk1.7.0_45
4.使用mvn package命令打包的時候會出現編碼警告,導致打包不成功,警告如下:
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!

解決問題的辦法是在POM檔案中加入如下配置:

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

    <properties>
         <project.build.sourceEncoding>
             UTF-8
         </project.build.sourceEncoding>
    </properties>