Maven使用過程中遇到的問題,及解決方案

  1. 多模塊項目的項目依賴關係,定義編譯順序
          <!--

       dmo一般定義bean

       commons裏面一般定義工具類

       biz裏面是業務邏輯

       provider是對外開放的接口

       main是服務的啓動

       鑑於代碼如此分佈,依賴關係一般也是按此順序,所以在modules中定義模塊,需要根據依賴順序安排先後

   -->

<modules>

   <module>service-outgate-dmo</module>

   <module>service-outgate-commons</module>

   <module>service-outgate-biz</module>

   <module>service-outgate-provider</module>

   <module>service-outgate-main</module>

</modules>

 

<!--

模塊之間存在依賴關係,需要弄清楚模塊之間的依賴順序,編譯的時候被依賴者放到modules元素中的靠前位置。

模塊如果互相依賴,要注意編譯順序。

   儘量避免模塊之間的循環依賴

-->

<modules>

   <module>service-video-dmo</module>

   <module>service-video-commons</module>

   <module>service-video-biz</module>

   <module>service-video-provider</module>

   <module>service-video-main</module>

</modules>

 

<!--

   maven多級項目,需要考慮同級父項目的子項目間的依賴關係,並行開發的服務,最好中間不要產生依賴。

   不同業務的請求和響應對象,在分項目的時候,不同業務之前的bean儘量不要產生依賴。子項目間的複雜

   依賴關係,後導致後期項目編譯依賴過於繁瑣。導致pom文件的依賴不好定義,會使之往更糟糕的方向發展。

-->

<modules>

   <module>service-user</module>

   <module>service-outgate</module>

   <module>service-video</module>

   <module>service-msg</module>

</modules>

 

以上可能出現的問題就是,編譯找不到類的方法。

 

  1. 針對Nexus中依賴有pom沒有jar

在Nexus中依賴有pom沒有jar,一般pom裏面都是有jar的地址信息,maven去下載的時候下載不下來,可能是因爲網絡,可能是因爲jar服務提供上收費,這種情況下,一般建議本地安裝jar到Nexus,不到第三方下載,將jar安裝在Nexus的thirtyParty目錄

  1. 依賴重複的情況

      

  1. Maven內置的變量

${basedir} 項目根目錄 

${project.build.directory} 構建目錄,缺省爲target 

${project.build.outputDirectory} 構建過程輸出目錄,缺省爲target/classes 

${project.build.finalName} 產出物名稱,缺省爲${project.artifactId}-${project.version} 

${project.packaging} 打包類型,缺省爲jar 

${project.xxx} 當前pom文件的任意節點的內容 

 

  1. 是選擇Maven編譯插件進行編譯,還是使用Pom默認的,爲什麼要配置編譯插件

 

  1. 資源文件怎麼打包,怎麼配置資源文件的打包

 

 

  1. Profile環境是怎麼選擇的

       Profile爲不同的環境可以配置不同的打包要求,在profile中可以覆蓋pom中的幾乎所有元素。在打包運行的時候需要在命令行傳參指定運行那個Profile,這個參數就是profile的id.

<profile>

   <id>local</id>

   <properties>

      <context.conf.path>classpath:config/properties/config_local.properties</context.conf.path>

   </properties>

   <activation>

      <activeByDefault>true</activeByDefault>

   </activation>

</profile>



<profile>

   <!-- 開發環境 -->

   <id>dev</id>

   <properties>

      <context.conf.path>classpath:config/properties/config_dev.properties</context.conf.path>

   </properties>

</profile>

<profile>

   <!-- 測試環境 -->

   <id>test</id>

   <properties>

      <context.conf.path>classpath:config/properties/config_test.properties</context.conf.path>

   </properties>

</profile>

</profiles>


假如打包本地環境,命令爲:

       mvn clean install –Plocal

此種方式,是手動激活,指定運行某種環境構建。

可以通過在profile中配置激活策略,當構建環境滿足某些構建策略時,進行某個profile的構建。激活配置放在上面加粗標紅的元素裏。

 

Profile可以單獨成立文件,引入到pom。也可以在私服的settings.xml中設置全局的profile,被所有項目共享。

  1. pluginspluginManagement

       pluginManagerment一般使用在父pom中,用來管理所有子項目需要用到的插件。在父pom中,聲明插件所有的基本信息(版本號,JDK版本等等),在子pom中使用使用插件只需groupIdartifactId即可,無需配置其他的額外信息,這樣可以做到所有子項目使用同樣的插件配置。如果子項目,需要使用不同的參數配置,在子pom中可以自行聲明。

在父pom中管理所有子項目可能需要使用到的插件,子項目不聲明使用父pom中的插件,則子項目就不會用到該插件。

父pom中的配置:

<pluginManagement>

    <plugins>

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-source-plugin</artifactId>

            <version>2.1</version>

            <configuration>

                <attach>true</attach>

            </configuration>

            <executions>

                <execution>

                    <phase>compile</phase>

                    <goals>

                        <goal>jar</goal>

                    </goals>

                </execution>

            </executions>

        </plugin>

    </plugins>

</pluginManagement>

 

子pom中的配置:

<plugins>

    <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-source-plugin</artifactId>

    </plugin>

</plugins>
 

pluginManagement裏只是聲明依賴,並不實現引入,因此子項目需要顯式的聲明需要用的依賴。Plugins則相反,聲明的都會引入。

DependencysdependencyManager是同一樣的道理。

      

  1. Maven打包,配置文件亂碼問題

     要找出文件亂碼的根源,就需要知道編碼的流程。從編寫文件到打成發佈包,中間經歷幾次轉碼。

     首先編寫文件,編輯器或IDE會默認爲文件指定編碼。

     其次文件會經過編譯器編譯,不過配置文件,應該不會被編譯器處理。

     Copy文件到要打的包中,這個過程maven是怎麼獲取編碼方式的,默認取機器編碼,還是獲取項目文件編碼,或者是在pom文件中指定編碼變量供其讀取使用。

     Maven獲取編碼的順序應該是:

如果爲指定project.build.sourceEncoding編碼,maven使用默認,指定則使用指定。插件也可以指定編碼,如果插件指定了編碼,則使用插件自身指定的編碼。
<properties>

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

      </properties>
  
 <configuration>
      <encode>utf-8</encode>  
</configuration> 

 

  1. Redhat-7關閉防火牆的辦法,一般端口不能訪問都是防火牆的問題。

在之前的版本中關閉防火牆等服務的命令是

 

 

 

service iptables stop

/etc/init.d/iptables stop

 

 

在RHEL7中,其實沒有這個服務

 

 

 

[root@rhel7 ~]# cat /etc/redhat-release

Red Hat Enterprise Linux Server release 7.0 (Maipo)

[root@rhel7 ~]# service iptables stop

Redirecting to /bin/systemctl stop  iptables.service

[root@rhel7 ~]# /etc/init.d/iptables stop

-bash: /etc/init.d/iptables: No such file or directory

 

 

原來在RHEL7開始,使用systemctl工具來管理服務程序,包括了service和chkconfig

systemctl list-unit-files|grep enabled

 

[root@rhel7 ~]# systemctl stop firewalld.service

[root@rhel7 ~]# systemctl disable firewalld.service

[root@rhel7 ~]# systemctl status firewalld.service

 

 

啓動一個服務:systemctl start firewalld.service

關閉一個服務:systemctl stop firewalld.service

重啓一個服務:systemctl restart firewalld.service

顯示一個服務的狀態:systemctl status firewalld.service

在開機時啓用一個服務:systemctl enable firewalld.service

在開機時禁用一個服務:systemctl disable firewalld.service

查看服務是否開機啓動:systemctl is-enabled firewalld.service;echo $?

查看已啓動的服務列表:systemctl list-unit-files|grep enabled

 

  1. Maven-war-plugin的使用

正常打包war項目時,只需標示項目打包類型即可實現web項目的war打包。但是項目不知爲何單獨聲明war-plugin,其指定的不同屬性導致打包的不同類型。

<plugin>

   <groupId>org.apache.maven.plugins</groupId>

   <artifactId>maven-war-plugin</artifactId>

   <version>2.2</version>

   <configuration>

      <warName>etopmiddle-oms-web</warName>

      <!--是否將war項目打成jar包,如果設置爲TRUE則將web項目打成jar,war中包含配置文件和lib,

      設置爲false,則是常規war-->

      <archiveClasses>false</archiveClasses>

      <webResources>

         <resource>

            <!-- this is relative to the pom.xml directory -->

            <directory>src/main/resources</directory>

            <targetPath>WEB-INF/classes</targetPath>

            <!-- the list has a default value of ** -->

            <includes>

               <include>**</include>

            </includes>

            <filtering>true</filtering>

         </resource>

      </webResources>

   </configuration>

</plugin>

 

以上即爲項目中的war-plugin的聲明。其意圖很明顯,是將war中的class文件打包成jar,然後在class的jar作爲lib引用,war中只包含配置文件。但是這樣打包,會引起配置文件的亂碼。避免亂碼的最好方式就是不使用非英文。註釋掉該配置,項目打包恢復正常。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章