java進階之-Maven,svn,git,maven合拼多個項目


git的使用介紹(寫很容易懂得哦)

繆雪峰寫的git介紹我看完感覺特別好:https://www.liaoxuefeng.com/wiki/896043488029600

maven合拼多個項目(寫得很好哦)

MAVEN作用:統一開發規范與工具;統一管理jar包

1.下載MAVEN  下載綠色版的面安裝

 

2.環境配置

  eclipse想要用maven需要加載maven插件才能對項目管理:分類,布局

  1)環境變量

    MAVEN_HOME:C:\java\apache-maven-3.2.5(maven文件根路徑)

    Path:添加%MAVEN_HOME%/bin

  2)配置文件:config/setting.xml

    1)修改更新到本地資源庫:  <localRepository>C:\java\apache-maven-3.2.5\repository</localRepository>

    2)修改默認的中央倉庫鏡像:在settings.xml文件中的“<mirrors>

    之后項目中需要的jar包會先從本地資源庫中查找,沒有的話就去默認的中央倉庫鏡像中查找

  eclipse適配:最新版的自己就適配maven,只需配置settings.xml的位置就好了;若是不是最新版的還需要配置Installations定位到Maven的根路徑

 

3.使用方法

  Maven常用命令: 

1. 創建Maven的普通java項目: 
mvn archetype:create -DgroupId=packageName -DartifactId=projectName 
2. 創建Maven的Web項目: 
mvn archetype:create -DgroupId=packageName -DartifactId=webappName-DarchetypeArtifactId=maven-archetype-webapp 
3. 編譯源代碼: mvn compile 
4. 編譯測試代碼:mvn test-compile 
5. 運行測試:mvn test 
6. 產生site:mvn site 
7. 打包:mvn package 
8. 在本地Repository中安裝jar:mvn install 
9. 清除產生的項目:mvn clean 
10. 生成eclipse項目:mvn eclipse:eclipse 
11. 生成idea項目:mvn idea:idea 
12. 組合使用goal命令,如只打包不測試:mvn -Dtest package 
13. 編譯測試的內容:mvn test-compile 
14. 只打jar包: mvn jar:jar 
15. 只測試而不編譯,也不測試編譯:mvn test -skipping compile -skipping test-compile 
( -skipping 的靈活運用,當然也可以用於其他組合命令) 
16. 清除eclipse的一些系統設置:mvn eclipse:clean
View Code

4.想要給項目添加

  不用直接給項目添加jar包了可以直接在pom.xml中配置jar包,maven項目會自動從本地資源庫中下載或者去maven中央倉庫中查找下載

其中groupId是com,cn類似的,artifatId是項目名稱
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency>

5.Spring+Mybatis+SpringMVC+Maven+MySql搭建實例

鏈接:http://pan.baidu.com/s/1c163fbi 密碼:mk4i

6.pom(Project Object Model) 

  1)pom.xml三個必須的字段:<groupId><artifactId><version>

  groupId:工程組的標識;artifactId:工程標識;version:工程版本標識

    <groupId>com.companyname.project-group</groupId>

    <artifactId>project</artifactId>

    <version>1.0</version>
View Code

  2)plugins

  3)goals

  4)

7.生命周期

  Maven有三套相互獨立的生命周期:生命周期哪個步驟需要執行一般執行命令行  

  • Clean Lifecycle 在進行真正的構建之前進行一些清理工作。
  • Default Lifecycle 構建的核心部分,編譯,測試,打包,部署等等。
  • Site Lifecycle 生成項目報告,站點,發布站點。

   1)Clean Lifecycle:

    1.pre-clean  執行一些需要在clean之前完成的工作

    2.clean  移除所有上一次構建生成的文件

    3.post-clean  執行一些需要在clean之后立刻完成的工作

    注意:執行mvn post-clean會自動執行完pre-clean,clean

  2)Site Lifecycle:

    1.pre-site     執行一些需要在生成站點文檔之前完成的工作

    2.site    生成項目的站點文檔

    3.post-site     執行一些需要在生成站點文檔之后完成的工作,並且為部署做准備

    4.site-deploy     將生成的站點文檔部署到特定的服務器上

    注意:常用到的是site階段和site-deploy階段

  3)Default Lifecycle:Maven最重要的生命周期,絕大多數工作都在這里

•    validate
•    generate-sources
•    process-sources
•    generate-resources
•    process-resources     復制並處理資源文件,至目標目錄,准備打包。
•    compile     編譯項目的源代碼。
•    process-classes
•    generate-test-sources 
•    process-test-sources 
•    generate-test-resources
•    process-test-resources     復制並處理資源文件,至目標測試目錄。
•    test-compile     編譯測試源代碼。
•    process-test-classes
•    test     使用合適的單元測試框架運行測試。這些測試代碼不會被打包或部署。
•    prepare-package
•    package     接受編譯好的代碼,打包成可發布的格式,如 JAR 。
•    pre-integration-test
•    integration-test
•    post-integration-test
•    verify
•    install     將包安裝至本地倉庫,以讓其它項目依賴。
•    deploy     將最終的包復制到遠程的倉庫,以讓其它開發人員與項目共享。

     注意:運行任何一個階段的時候,它前面的所有階段都會被運行,這也就是為什么我們運行mvn install 的時候,代碼會被編譯,測試,打包

    每個階段詳解: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

8.構建配置文件

  作用:通過指定參數來做特定的事,可為不同的環境(生產環境和開發),定制構建方式

  三種構建配置文件方式:項目級(Per Project),用戶級(Per User),全局(Gobal) 

  配置文件一般存放中,

  構建配置文件采用的是<profiles>節點

  1)項目級

    直接在pom.xml中節點<profiles>配置不同環境指定不同的構建方案

    mvn test -Ptest:第一個test為Maven生命周期階段,第2個test為為構建配置文件指定的<id>參數,這個參數通過-P來傳輸

  2)用戶級

    %USER_HOME%/.m2;%M2_HOME%/conf/目錄下的settings.xml文件;

    增加<activeProfiles>屬性

<settings 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/xsd/settings-1.0.0.xsd">
   ...
   <activeProfiles>
      <activeProfile>test</activeProfile>
   </activeProfiles>
</settings>
View Code

    mvn test:自動定位到activeProfiles指定的test配置文件

  3)全局(設置變量方式)

<profile>
   <id>test</id>
   <activation>
      <property>
         <name>env</name>
         <value>test</value>
      </property>
   </activation>
</profile>
View Code

    mvn test -Denv=test:env變量value為test的那個配置文件

9.倉庫

  分為:本地倉庫;中央倉庫;遠程倉庫

  1)本地倉庫:

    setting,xml配置localRepository節點

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
   http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <localRepository>C:/MyLocalRepository</localRepository>
</settings>
View Code

  2)中央倉庫:

    maven的一個中央倉庫由maven市區維護http://search.maven.org/#browse

    也在setting.xml中配置repositories節點配置

<repositories>        
  <repository>        
    <id> central</id>        
    <name> Maven Repository Switchboard</name>        
    <layout> default</layout>        
    <url> http://repo1.maven.org/maven2</url>        
    <snapshots>        
      <enabled> false</enabled>        
    </snapshots>        
  </repository>        
</repositories> 
View Code

  3)遠程倉庫:

    在pom.xml中配置repositories節點

   <repositories>
      <repository>
         <id>companyname.lib1</id>
         <url>http://download.companyname.org/maven2/lib1</url>
      </repository>
      <repository>
         <id>companyname.lib2</id>
         <url>http://download.companyname.org/maven2/lib2</url>
      </repository>
   </repositories>
View Code

  依賴搜索順序:先查本地服務沒有jar包時-》中央倉庫沒有jar包時-》遠程倉庫沒有jar包時-》報錯 

10.插件plugins

  Maven實際上就是依賴插件執行的框架;通常被用來

創建 jar 文件
創建 war 文件
編譯代碼文件
代碼單元測試
創建工程文檔
創建工程報告
View Code

  語法:mvn [plugin-name]:[goal-name];如:mvn compiler:compile

  分類:Build plugins構建時執行並配置;Reporting plugins網站生成過程執行並配置

  常用的插件:

插件            描述
clean            構建之后清理目標文件。刪除目標目錄。
compiler    編譯 Java 源文件。
surefile    運行 JUnit 單元測試。創建測試報告。
jar            從當前工程中構建 JAR 文件。
war            從當前工程中構建 WAR 文件。
javadoc    為工程生成 Javadoc。
antrun    從構建過程的任意一個階段中運行一個 ant 任務的集合。        
View Code

  pom.xml配置:可以配置executions節點執行插件用途

<plugins>
   <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.1</version>
   <executions>
      <execution>
         <id>id.clean</id>
         <phase>clean</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>clean phase</echo>
            </tasks>
         </configuration>
      </execution>     
   </executions>
   </plugin>
</plugins>
View Code

11.代碼創建工程:

  C:\MVN>mvn archetype:generate -DgroupId=com.companyname.bank -DartifactId=consumerBanking -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

12.外部依賴在pom.xml中配置dependencies節點

       <dependencies>
          <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>3.8.1</version>
             <scope>test</scope>
          </dependency>

          <dependency>
             <groupId>ldapjdk</groupId>
             <artifactId>ldapjdk</artifactId>
             <scope>system</scope>
             <version>1.0</version>
             <systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
          </dependency>
       </dependencies>
View Code

  其中scope:指定作用域;systemPath:jar包下載到指定路徑

13.創建工程文檔;工程模板

  命令行:C:\MVN>mvn site;mvn archetype:generate

14.快照:服務端的jar包老是更新最新版本,用它的pom.xml就需要不斷地更新配置很麻煩就出現了快照

  1)過程:提供jar包的服務端配置jar包時<version>配置的版本名字加上-SNAPSHOT:如<version>1.0-SNAPSHOT</version>

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>data-service</groupId>
  <artifactId>data-service</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>health</name>
  <url>http://maven.apache.org</url>
  <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  </project>
View Code

        使用這個jar包快照pom.xml中配置:  

  <dependencies>
     <dependency>
     <groupId>data-service</groupId>
         <artifactId>data-service</artifactId>
         <version>1.0-SNAPSHOT</version>
         <scope>test</scope>
     </dependency>
  </dependencies>
View Code

  2)命令行:強制獲得最新快照:mvn clean package -U

15.構建自動化

  一旦一個工程創建成功,其相關依賴工程也要開始重新構建工程從而保證其依賴項的穩定性

  場景:一個 bus-core-api更新后,希望app-web-ui,app-desktop-ui兩個從線也自動更新

app-web-ui 
<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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>app-web-ui</groupId>
   <artifactId>app-web-ui</artifactId>
   <version>1.0</version>
   <packaging>jar</packaging>
   <dependencies>
      <dependency>
      <groupId>bus-core-api</groupId>
         <artifactId>bus-core-api</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>
   </dependencies>
</project>

app-desktop-ui 
<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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>app-web-ui</groupId>
   <artifactId>app-web-ui</artifactId>
   <version>1.0</version>
   <packaging>jar</packaging>
   <dependencies>
      <dependency>
      <groupId>bus-core-api</groupId>
         <artifactId>bus-core-api</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>
   </dependencies>
</project>

bus-core-api
<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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>bus-core-api</groupId>
   <artifactId>bus-core-api</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging>   
</project>
View Code

    1)在三個線中配置快照-》2)

  • 在 bus-core-api 的 pom 文件里添加一個編譯目標來提醒 app-web-ui 工程和 app-desktop-ui 工程啟動創建。->C:\MVN\bus-core-api>mvn clean package -U
  • <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/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>bus-core-api</groupId>
       <artifactId>bus-core-api</artifactId>
       <version>1.0-SNAPSHOT</version>
       <packaging>jar</packaging>
       <build>
       <plugins>
       <plugin>
       <artifactId>maven-invoker-plugin</artifactId>
       <version>1.6</version>
          <configuration>
             <debug>true</debug>
             <pomIncludes>
                <pomInclude>app-web-ui/pom.xml</pomInclude>
                <pomInclude>app-desktop-ui/pom.xml</pomInclude> 
             </pomIncludes>
          </configuration>
          <executions>
             <execution>
                <id>build</id>
                <goals>
                   <goal>run</goal>
                </goals>
             </execution>
          </executions>
       </plugin>
       </plugins>
       <build>
    </project>
    View Code

     

16.依賴管理:沒啥說的看看就是maven對多個pom之間的依賴關系

 

17.自動化部署:只用pom.xml配置就能全部被自動部署

  使用Maven發布的插件

<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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>bus-core-api</groupId>
   <artifactId>bus-core-api</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>jar</packaging> 
   <scm>
      <url>http://www.svn.com</url>
      <connection>scm:svn:http://localhost:8080/svn/jrepo/trunk/
      Framework</connection>
      <developerConnection>scm:svn:${username}/${password}@localhost:8080:
      common_core_api:1101:code</developerConnection>
   </scm>
   <distributionManagement>
      <repository>
         <id>Core-API-Java-Release</id>
         <name>Release repository</name>
         <url>http://localhost:8081/nexus/content/repositories/
         Core-Api-Release</url>
      </repository>
   </distributionManagement>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.0-beta-9</version>
            <configuration>
               <useReleaseProfile>false</useReleaseProfile>
               <goals>deploy</goals>
               <scmCommentPrefix>[bus-core-api-release-checkin]-<
               /scmCommentPrefix>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>
View Code
元素    描述
SCM    配置 SVN 的路徑,Maven 將從該路徑下將代碼取下來。
repository 成功構建出來的 WAR
/EAR/JAR 或者其他的構建結果存放的路徑。 plugins maven-release-plugin 用以自動化部署的過程。

18.Apache Maven Elipse IDEApache Maven NetBeansApache Maven IntelliJ IDEA

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

SVN:管理項目,用戶團隊合作

  客戶端配置:需要注意的是不要選擇command....那一欄

  1.新建庫TortoiseSVN - Create repository here;

  2.導入項目:import(在文件夾下空白處右鍵);checkout一般我們用的是checkout(文件夾直接checkout)能將最新版本更新到本地

  3.更新工作目錄:

    空白處點擊鼠標右鍵,選擇“SVN Update“;顯示更新了哪些內容,庫版本是多少

  4提交工作目錄:

    在做了修改,需要保存到庫中時,用到提交操作:SVN Commit

  5.svn在eclipse中使用

    1).配置svn環境(Subclipse

    2)上傳項目到project

     在eclipse視圖資源庫中(Window/Show View)

    3)在svn資源庫空白位置選擇新建資源庫位置4)填好資源庫位置服務器地址5)導入成功后出現導入的資源庫

    4)右鍵project/team/share project

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM