mvn cli 搭建項目架構

建立如圖所示目錄結構

在system-parent建立以下目錄web

├─system-daoapache

├─system-domain瀏覽器

├─system-serviceapp

└─system-webdom

建立system-parent

mvn archetype:generate -DgroupId=com.hbb0b0 -DartifactId=system-parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=localwebapp

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.hbb0b0</groupId>
  <artifactId>system-parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>system-parent</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <modules>
    <module>system-domain</module>
    <module>system-dao</module>
    <module>system-service</module>
    <module>system-web</module>
  </modules>
</project>

說明

  • system-parent 只須要 pom文件
  • 修改 jar pom
  • 其中 節點的內容是子項目建立後本身加上去的,不用手工加

建立子項目 system-domain

mvn archetype:generate -DgroupId=com.hbb0b0 -DartifactId=system-domain -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=localjsp

pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.hbb0b0</groupId>
    <artifactId>system-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>system-domain</artifactId>
  <name>system-domain</name>
  <packaging>jar</packaging>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

說明

  • 去掉子項目 ,去掉以後表示子項目的version,groupid 繼承自 system-parent pom
  • 添加 jar
  • 建立子項目 system-do

mvn archetype:generate -DgroupId=com.hbb0b0 -DartifactId=system-dao -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=localmaven

pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.hbb0b0</groupId>
    <artifactId>system-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>system-dao</artifactId>
  <packaging>jar</packaging>
  <name>system-dao</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.hbb0b0</groupId>
      <artifactId>system-domain</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</project>

說明

  • 去掉子項目 ,去掉以後表示子項目的version,groupid 繼承自 system-parent pom
  • 添加 jar
  • system-dao 依賴 system-domain ,因此dependency 添加 system-domain 依賴

建立子項目 system-service

mvn archetype:generate -DgroupId=com.hbb0b0 -DartifactId=system-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeCatalog=localui

pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.hbb0b0</groupId>
    <artifactId>system-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>system-service</artifactId>
  <name>system-service</name>
   <packaging>jar</packaging>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.hbb0b0</groupId>
      <artifactId>system-dao</artifactId>
      <version>${project.version}</version>
    </dependency>
    
  </dependencies>
</project>

說明

  • 去掉子項目 ,去掉以後表示子項目的version,groupid 繼承自 system-parent pom
  • 添加 jar
  • service 直接依賴 system-dao ,system-dao依賴system-domain,dependency 添加直接依賴 system-dao ,不須要添加system-domain 的依賴

建立子項目 system-web

mvn archetype:generate -DgroupId=com.hbb0b0 -DartifactId=system-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false -DarchetypeCatalog=localurl

pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.hbb0b0</groupId>
    <artifactId>system-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>system-web</artifactId>
  <packaging>war</packaging>
  <name>system-web Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.hbb0b0</groupId>
      <artifactId>system-service</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>system-web</finalName>
    <pluginManagement>
        <!--配置Jetty-->
        <plugins>
           <plugin>
            <groupId>org.mortbay.jetty</groupId>   
             <artifactId>maven-jetty-plugin</artifactId>
            </plugin>
         </plugins>
     </pluginManagement>
  </build>
</project>

說明

  • 去掉子項目 ,去掉以後表示子項目的version,groupid 繼承自 system-parent pom
  • 添加 jar
  • 添加 system-service 依賴
  • 添加 jetty 插件 ,這樣作的目的,讓web項目直接在命令行運行
    --修改 jsp文件 添加項目目錄結構

清理與編譯

在 system-parent目錄運行

mvn clean install

執行成功以後 能夠看到如下輸出

[INFO] system-parent ...................................... SUCCESS [ 0.562 s]

[INFO] system-domain ...................................... SUCCESS [ 3.638 s]

[INFO] system-dao ......................................... SUCCESS [ 0.956 s]

[INFO] system-service ..................................... SUCCESS [ 0.956 s]

[INFO] system-web Maven Webapp ............................ SUCCESS [ 0.795 s]

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 7.078 s

[INFO] Finished at: 2018-04-23T22:42:37+08:00

[INFO] Final Memory: 20M/192M

[INFO] ------------------------------------------------------------------------

運行web 項目

mvn jetty:run

運行成功以後控制檯會輸出一下信息

[INFO] jetty-6.1.26

[INFO] No Transaction manager fo

[INFO] Started SelectChannelConn

[INFO] Started Jetty Server

maven web project

瀏覽器結果

http://localhost:8080/system-web/

---system-dao

---system-domain

---system-service

---system-web

相關文章
相關標籤/搜索