maven創建父子工程、聚合工程及問題解決

springboot創建父子工程、聚合工程

開發工具:IntellJ IDEA 2017

springboot創建父子工程、聚合工程及搭建框架過程中遇到的問題解決

項目名稱

wyait父工程【父】:wyait-parent(用於統一依賴版本管理) 
wyait通用工程【子】:wyait-common(統一保存通用工具類) 
wyait-web工程【子】:wyait-web(聚合工程)

項目框架簡述

wyait-web 項目框架設計簡述:

  • wyait-web-pojo:保存pojo和entity實體類;
  • wyait-web-dao:數據訪問層,與底層 MySQL進行數據交互(依賴pojo);
  • wyait-web-service:相對具體的業務邏輯服務層(依賴mapper/pojo);
  • wyait-web-controller:對訪問控制進行轉發,各類基本參數校驗,或者不復用的業務簡單處理等(依賴service/pojo);
  • wyait-web-webapp:項目啓動Application類、配置類、靜態資源文件(依賴controller/pojo)。

項目源碼

github:https://github.com/wyait/web.git 
碼雲:https://gitee.com/wyait/web.git

注意,源碼中沒有提交空白目錄,有些src/main/java/、src/main/resources/等目錄需要大家手動創建!!!

初始技術依賴包括:

  • springboot
  • spring
  • mybatis
  • thymeleaf模版
  • redis
  • httpclient
  • oval校驗
  • commons包
  • devTools熱部署
  • 靜態資源配置(詳見:MyWebMvcConfig類)
  • druid數據庫連接池
  • pagehelper分頁
  • log4j2

靜態資源配置說明

MyWebMvcConfig類片段:

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(“/css/**”).addResourceLocations(“/css/”);
        registry.addResourceHandler(“/images/**”).addResourceLocations(“/images/”);
        registry.addResourceHandler(“/js/**”).addResourceLocations(“/js/”);
        //配置中的file:表示是一個具體的硬盤路徑,其他的配置指的是系統環境變量
        registry.addResourceHandler(“/img/**”).addResourceLocations(“file:D:/demo-images/”);
        super.addResourceHandlers(registry);
    }

注意:使用IntellJ IDEA開發工具時,有個問題,見文末。

RedisUtil

redis的使用,參考wyait-common項目中RedisUtil類中的API方法;
參考博客: 
spring boot 1.5.9 整合redis:http://blog.51cto.com/wyait/2048478

HttpService

httpClient操作,參考midd-common項目中HttpService類中的API方法,結果封裝在HttpResult類中

搭建項目框架

wyait-parent(版本管理)

每個平臺都有一個parent項目,用於項目依賴版本統一管理

創建wyait-parent項目

  • File-->new project-->maven: 
    image
  • next:輸入maven座標;
  • next:Project name: wyait-parent
  • Finish
  • 刪除src/目錄。

項目結構如下: 
image

導入依賴

注意:

  1. springboot在創建單應用項目的時候,有默認的<parent>依賴;我們在自定義wyait-parent項目時,如果直接以springboot的parent作爲wyait-parent項目的<parent>父依賴管理的話,子項目會由於存在兩個parent而報錯,無法導入依賴。 
    解決方案如下:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>${spring.boot.version}</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

使用spring-boot-dependencies依賴對springboot的依賴包進行統一管理。

  1. maven項目的pom中的標籤的含義,自行google、百度瞭解 //TODO
... ...

    <groupId>com.wyait.parent</groupId>
    <artifactId>wyait-parent</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging><!--父模塊打包類型必須爲pom-->

    <properties>
        <!--依賴版本號管理-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8
        </project.reporting.outputEncoding>
        <java.version>1.7</java.version>
        <!--spring依賴版本控制(和spring-boot-parent版本保持一致)-->
        <springframework.version>4.3.13.RELEASE</springframework.version>
        <!--spring-boot-parent版本號,通過spring-boot管理其他第三方依賴版本-->
        <spring.boot.version>1.5.9.RELEASE</spring.boot.version>
        <mybatis.version>1.3.1</mybatis.version>
        <druid.version>1.1.5</druid.version>
        <pagehelper.version>1.2.3</pagehelper.version>
        <commons.lang3.version>3.6</commons.lang3.version>
        <commons.io.version>2.5</commons.io.version>
        <oval.version>1.86</oval.version>
    </properties>
    <!--管理依賴jar包-->
    <dependencyManagement>
        <dependencies>
            <!-- 統一管理Spring依賴 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${springframework.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
           ...<!--此處省略,詳見源碼!源碼地址詳見文末-->
        </dependencies>
    </dependencyManagement>

    <!--統一插件配置版本管理 TODO-->
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <!--指定編譯時的jdk版本1.7 -->
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

wyait-common(通用工具類項目)

用於存放通用的工具類;
wyait-common項目,統一保存通用工具類
涉及到bean注入,需要在wyait-web的Application啓動類中,添加註解:

@SpringBootApplication
//@ComponentScan用於配置掃描com.wyait.web之外的包下面的類
@ComponentScan(basePackages={"com.wyait"})
public class WyaitWebApplication {

    public static void main(String[] args) {
        SpringApplication sa=new SpringApplication(WyaitWebApplication.class);
        // 禁用devTools熱部署
        //System.setProperty("spring.devtools.restart.enabled", "false");
        // 禁用命令行更改application.properties屬性
        sa.setAddCommandLineProperties(false);
        sa.run(args);
    }
}

創建wyait-common

方法和創建wyait-parent一致,注意不刪除src。

pom依賴


    <parent>
        <groupId>com.wyait.parent</groupId>
        <artifactId>wyait-parent</artifactId>
        <version>1.0.0</version>
    </parent>

    <groupId>com.wyait.common</groupId>
    <artifactId>wyait-common</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <dependencies>
       ...<!--此處省略,根據wyait-common需要導入相應的依賴即可-->
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

wyait-web聚合工程

本demo爲了方便,前期所有第三方依賴統一加在pom工程裏面。比如:都放在wyait-web的pom文件中。上線前,再統一做調整,分別配置依賴。
建議前期就分配module各項目需要的依賴,避免引入不必要的。

創建wyait-web

操作和創建wyait-common一致。

pom依賴

  1. 項目內版本管理,通過配置properties實現;
  2. 引入通用工具項目wyait-common;
  3. 解決項目啓動後,webapp/資源目錄編譯之後訪問不到的問題。
...

    <groupId>com.wyait.web</groupId>
    <artifactId>wyait-web</artifactId>
    <version>${wyait.web.version}</version>
    <packaging>pom</packaging>
    <parent>
        <groupId>com.wyait.parent</groupId>
        <artifactId>wyait-parent</artifactId>
        <version>1.0.0</version>
    </parent>
     <!--聚合子模塊-->
    <modules>
    </modules>

    <properties>
        <!--wyait-web項目版本管理-->
        <wyait.web.version>1.0.0</wyait.web.version>
        <wyait.common.version>1.0.0</wyait.common.version>
    </properties>

    <dependencies>
        <!--引入common依賴-->
        <dependency>
            <groupId>com.wyait.common</groupId>
            <artifactId>wyait-common</artifactId>
            <version>${wyait.common.version}</version>
        </dependency>

        ...
        <!--詳見源碼;源碼鏈接在文末-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!-- 使用IDEA開發工具時,需要加上該resources配置,解決webapp/資源目錄無效的問題 -->
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <!--編譯的時候把webapp文件放到resources下,必須要放在此目錄下才能被訪問到 -->
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

創建wyait-web-pojo

選中pom項目wyait-web,右鍵:new --> module --> maven :

image

finish。

項目結構: 
image

pom依賴

 <parent>
        <artifactId>wyait-web</artifactId>
        <groupId>com.wyait.web</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wyait.web.pojo</groupId>
    <artifactId>wyait-web-pojo</artifactId>
    <version>${wyait.web.version}</version>
    <packaging>jar</packaging>

創建wyait-web-service、wyait-web-dao、wyait-web-controller項目一樣。pom依賴根據依賴關係,自行調整,詳見源碼。

創建wyait-web-webapp

方式一

第一種,創建方式和wyait-web-pojo一樣;

創建項目完成後,另外需要幾步操作:

  1. 新建src/main/webapp/文件目錄,用於存放靜態資源文件和頁面;
  2. src/main/java/目錄下,新建包com.wyait.web和com.wyait.web.config,存放springboot啓動Application類和相關配置類;
  3. src/main/resources/目錄下,保存application.properties、log4j2.xml等配置文件。 
    wyait-web-webapp項目架構如下: 
    image

注意:這裏WEB-INF/下面多了一個web.xml文件,或在pom文件中添加一個maven-war-plugin插件配置,下文會說明原因。

方式二

第二種,在選擇maven的時候,選中:Create from archetype,找到:maven-archetype-webapp,創建webapp項目; 
然後調整項目結構和依賴和方式一一致即可。//TODO

方式三

第三種,New --> project --> Spring Initializr創建springboot項目 --> 調整項目結構和依賴和上面保持一致即可。//TODO

測試

新建controller類、靜態文件和頁面,詳見源碼!

啓動測試:

http://127.0.0.1:8099/home 
image

IndexController中有midd-common項目中通用工具類的測試,也可自行編寫測試
其他技術依賴。//TODO

項目安全

  1. 用戶登錄後,設置的cookie值token必須加密處理;
  2. API接口安全,涉及用戶相關模塊的操作,後臺會校驗token;也可參考博客:java接口安全:http://blog.51cto.com/wyait/1920134
  3. 防止xss、sql注入等,後期統一處理。

項目檢出

eclipse

  1. 以eclipse爲例,找到SVN資源庫,輸入SVN地址,checkout:
  2. 檢出後,查看項目結構如下: 
    image
  3. 選中其中的module項目,右鍵 --> import: 
    image 
    選擇:Existing Maven Projects --> next --> finish;

就會導出module模塊wyait-web-controller,其他模塊操作一樣。

  1. 運行:
    在wyait-web-webapp項目src/main/java/com/wyait/web/目錄下,使用 WyaitWebApplication類中的main方法啓動項目。 
    也可配置tomcat啓動,注意項目路徑。//TODO

IntellJ IDEA

相比eclipse簡單一些,主要在check out過程中,要注意聚合項目的目錄層級結構(平行結構、父子結構)。//TODO

開發工具不同遇到的問題

eclipse和IntellJ IDEA開發工具不同遇到的問題。
使用IntellJ IDEA,必須在依賴中添加以下配置:

  1. 使用IDEA開發工具時,需要在wyait-web目錄下,新建src/main/java、src/main/resources空白目錄,否則啓動時報錯:
Caused by: java.lang.IllegalArgumentException: Folder 'D:\wyaitWorkspace\wyait-web\src\main\resources' must exist and must be a directory

加上即可解決;

  1. 使用IDEA開發工具時,註釋下面的依賴:
<!--使用IDEA開發工具時,註釋該依賴,否則啓動報錯;IDEA內置tomcat-->
            <!--<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>-->
  1. 使用IDEA開發時,爲了配置靜態資源文件放在webapp目錄下,需要新增如下配置:
<!-- 使用IDEA開發工具時,需要加上該resources配置,解決webapp/資源目錄無效的問題 -->
    <resources>
        <resource>
            <directory>src/main/webapp</directory>
            <!--編譯的時候把webapp文件放到resources下,必須要放在此目錄下才能被訪問到 -->
            <targetPath>META-INF/resources</targetPath>
            <includes>
                <include>**/**</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>

項目package時的問題

wyait-web-webapp要打包爲war時,項目src/main/webapp/WEB-INF/目錄下,如果沒有web.xml文件,打包會報錯:找不到WEB-INF/web.xml.

... ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.266 s
[INFO] Finished at: 2018-07-18T11:37:05+08:00
[INFO] Final Memory: 19M/184M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project mdd-admin: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 

解決方案

方案一:在項目WEB-INF/目錄下新建一個web.xml文件,用於項目package時使用,無其他用途。 
web.xml文件不需要增加任何配置,內容如下:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

方案二【推薦】:在pom.xml中添加依賴: 
注意Failed失敗中,maven-war-plugin的版本爲2.2。所以pom中配置的插件版本要對應一樣。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
</plugin>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章