maven小節,Nexus私服,構件打包發佈,動態資源過濾,自動部署到本地或遠程服務器

閱讀此文的前提,對Maven 有必定了解,熟悉pom文件基礎java

1:Nexus 創建私服 去下載nexus的war包格式的,最新版本的要使用高版本的web容器; 如:,下載後直接放到tomcat下 ,啓動運行mysql

登錄進去能夠看到默認有多個倉庫了

在此輸入圖片描述

手動創建倉庫 ,倉庫分類有1:宿主倉庫 2:代理倉庫 3:倉庫組 關於創建私服,也很簡單,不會的 推薦區看《Maven實戰》web

這裏是我建立的 本身的倉庫,也包含有默認倉庫 在此輸入圖片描述spring

2:pom.xml配置sql

<!-- lang: java -->
<!-- 爲此項目配置倉庫(repositories)和插件倉庫(pluginRepositories),
	這裏配置Nexus私服倉庫,配置在pom中,表示只在當前項目中有效 。
  	在實際應用中,咱們每每經過配置setting.xml使本機全部Maven項目都是用指定的私服 -->
<repositories>
  <repository>
      <id>dy_nexus_group</id>
      <name>dy_nexus_group</name>
      <url>http://localhost:8080/nexus/content/groups/dy_nexus_group</url>
      <releases>
          <enabled>true</enabled>
      </releases>
      <snapshots>
          <enabled>true</enabled>
      </snapshots>
  </repository>
</repositories>
<pluginRepositories>
  <pluginRepository>
      <id>dy_nexus_group</id>
      <name>dy_nexus_group</name>
      <url>http://localhost:8080/nexus/content/groups/dy_nexus_group</url>
      <releases>
          <enabled>true</enabled>
      </releases>
      <snapshots>
          <enabled>true</enabled>
      </snapshots>
  </pluginRepository>
</pluginRepositories>
<!-- 發佈構件 -->
<!-- Nexus的倉庫對於匿名用戶是隻讀的,爲了可以部署構件,還須要在setting.xml配置認證信息 -->
<distributionManagement>
	<repository>
		<id>dy_nexus_hosted_release</id>
		<name>dy_nexus_hosted_release</name>
		<url>http://localhost:8080/nexus/content/repositories/dy_nexus_hosted_release</url>
	</repository>
	<snapshotRepository>
		<id>dy_nexus_hosted_snapshot</id>
		<name>dy_nexus_hosted_snapshot</name>
		<url>http://localhost:8080/nexus/content/repositories/dy_nexus_hosted_snapshot</url>
	</snapshotRepository>
</distributionManagement>

<!-- Maven屬性 :包括內置屬性,pom屬性,setting屬性,自定義屬性,java系統屬性,環境變量屬性,參見p.280-->
<properties>
	<springframework.version>3.2.8</springframework.version>
</properties>

<!-- 資源過濾 -->
<profiles>
	<!-- 針對開發環境的數據庫設置 (開發環境)clean compile install -Pdy.dev -->
	<!-- spring配置中經過${db.driver}就能夠拿到此處的配置值了 -->
	<!-- Profile激活有多種方式詳見:p.285 -->
	<profile>
		<id>dy.dev</id>
		<properties>
			<myprop>dy.dev</myprop>
			<!-- <db.driver>com.mysql.jdbc.Driver</db.driver>
			<db.url>jdbc:mysql://127.0.0.3306/dev</db.url>
			<db.username>pom.dev</db.username>
			<db.password>pom.dev</db.password>
			<db.maxIdle>11</db.maxIdle>
			<db.maxActive>111</db.maxActive> -->
		</properties>
		<!-- 默認自動激活 -->
		<activation>
			<activeByDefault>true</activeByDefault>
		</activation>
	</profile>
	<!-- 針對開發環境的數據庫設置 (開發環境)clean compile install -Pdy.test -->
	<profile>
		<id>dy.test</id>
		<properties>
			<myprop>dy.test</myprop>
		</properties>
	</profile>
		<!-- 針對開發環境的數據庫設置 (開發環境)clean compile install -Pdy.prod -->
	<profile>
		<id>dy.prod</id>
		<properties>
			<myprop>dy.prod</myprop>
		</properties>
	</profile>
</profiles>

配置依賴: <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency>數據庫

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${springframework.version}.RELEASE</version>
    </dependency>
    <dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
		<scope>provided</scope>
	</dependency>
</dependencies>

構建項目:apache

<!-- 打包和發佈的最終名字-->
<finalName>hello-world</finalName> <!-- 配置過濾自定義的資源文件 -->
<filters>
    <filter>${project.basedir}/${myprop}.properties</filter>
</filters> <!-- 爲主資源目錄開啓過濾-->
<resources>
    <resource>
        <directory>${project.basedir}/src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>
<testResources>
    <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
        <filtering>true</filtering>
    </testResource>
</testResources>
<!-- Cargo是一組幫助用戶操做web容器的工具。其提供兩種本地部署的方式:standalone模式和existing模式。 -->
		<!-- standalone模式中:Cargo會從web容器的安裝目錄複製一份配置到用戶指定的目錄,
		而後在此基礎上部署應用。每次從新構建的時候,這個目錄被清空,全部配置從新清空。-->
		<!-- existing模式中:用戶須要指定現有web容器的配置目錄,而後Cargo會直接使用這些配置並將應用部署到其  對應的目錄。運行命令:cargo:run(須要配置pluginGroups 詳見setting.xml)-->
		<!-- 更多設置可參見官網: http://cargo.codehaus.org -->
<plugin>
			<groupId>org.codehaus.cargo</groupId>
			<artifactId>cargo-maven2-plugin</artifactId>
			<version>1.2.4</version>
			<configuration>
				<container>
					<containerId>tomcat7x</containerId>
					<home>F:/apache-maven-3.0.5-test/apache-tomcat-7.0.33</home>
				</container>
				<!-- <configuration>
					<type>standalone</type>
					<home>${project.build.directory}/tomcat7x</home>
					<properties>
						<cargo.servlet.port>8081</cargo.servlet.port>
					</properties>
				</configuration> -->
				
				<configuration>
					<type>existing</type>
					<home>F:/apache-maven-3.0.5-test/apache-tomcat-7.0.33</home>
				</configuration>
			</configuration>
<!-- lang: java -->

<!-- 部署至遠程服務器,前提是擁有該容器的相應管理員權限,配置以下: -->api

<configuration>
				<container>
					<containerId>tomcat7x</containerId>
					<type>remote</type>
				</container>
				<configuration>
					<!--遠程正在運行的服務器 -->
					<type>runtime</type>
					<properties>
						<cargo.hostname>192.168.168.50</cargo.hostname>
						<cargo.servlet.uriencoding>UTF-8</cargo.servlet.uriencoding>
						<cargo.remote.username>dengyang</cargo.remote.username>
						<cargo.remote.password>dengyang</cargo.remote.password>
						<cargo.tomcat.manager.url>
                                                                http://192.168.168.50:8080/manager
                                                     </cargo.tomcat.manager.url>
					</properties>
				</configuration>
			</configuration>
		</plugin>

...... tomcat7.x 的tomcat-user.xml 配置管理員帳戶(tomcat6配置還不太同樣) 以下:tomcat

<!-- lang: js -->
<role rolename="manager"/>  
<role rolename="manager-gui"/>  
<role rolename="manager-script"/>  
<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>	
<!-- lang: xml -->
<user username="dengyang" password="dengyang" roles="admin,admin-gui,admin-script,manager,manager-       script,manager-gui"/>

3:setting.xml配置服務器

<!-- lang: java -->
<localRepository>F:\JAVA_TOOLS\TOOLS\Maven\local_maven_repository</localRepository>
  本地倉庫地址
<servers>
 <!-- Nexus的倉庫對於匿名用戶是隻讀的,爲了可以部署構件,還須要在setting.xml配置認證信息, <server>中的id 和<repository>中的id徹底一致 -->
<server>
    <id>dy_nexus_hosted_release</id>
    <username>admin</username>
    <password>admin123</password>
</server>
<server>
    <id>dy_nexus_hosted_snapshot</id>
    <username>admin</username>
    <password>admin123</password>
</server>
 <!-- Another sample, using keys to authenticate.
 <server> 
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
 <passphrase>optional; leave empty if not used.</passphrase> 
</server> -->
</servers> 


        <!-- lang: java -->
        如下配置說明:本機全部maven項目私服配置倉庫(repositories)和插件倉庫(pluginRepositories),
        這樣配置除了會去私服nexus下載構件外,還會不時地訪問中央倉庫, 但咱們但願的是全部構件和依賴下載的請求都僅僅經過Nexus,以全面發揮私服的做用。 這時候就須要配置Maven鏡像了。
<profile>
<id>my_nexus</id>
<repositories>
    <repository>
        <id>dy_nexus_group</id>
        <name>dy_nexus_group</name>
        <url>http://localhost:8080/nexus/content/groups/dy_nexus_group</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <layout>default</layout>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>dy_nexus_group</id>
        <name>dy_nexus_group</name>
        <url>http://localhost:8080/nexus/content/groups/dy_nexus_group</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
</profile> -->

 去mirrors下,增長配置,並修改此處配置: 
<!--若是倉庫X能夠提供倉庫Y存儲的全部內容,那麼就能夠認爲X是Y的一個鏡像。 dy_nexus_group倉庫組中增長central代理倉庫以及其餘你本身的倉庫-->
 <mirrors>
<mirror>
    <id>my_nexus</id>
    <mirrorOf>*</mirrorOf>
    <name>maven mirror for my nexus</name>
    <url>http://localhost:8080/nexus/content/groups/dy_nexus_group</url>
</mirror>
</mirrors>
 <!-- 修改以上profile配置以下:修改後配置,即便用maven鏡像配置,這裏同時配置了插件倉庫,用不到也能夠不配置; 倉庫和插件倉庫id都爲central意味覆蓋了超級pom的中央倉庫的配置 -->
 <profile>
<id>my_nexus</id>
<repositories>
    <repository>
        <id>central</id>
        <name>central</name>
        <url>http://central</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <layout>default</layout>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>central</id>
        <url>http://central</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
</profile>

        <!-- 使用activeProfiles 將my_nexus私服激活 , activeProfile 對應profile 中的id,使用maven鏡像的時候 此處對應的是mirror中的id-->

<activeProfiles>
<activeProfile>my_nexus</activeProfile>
</activeProfiles>

        <!-- lang: xml -->
        使用cargo命令配置: <pluginGroups>
<pluginGroup>org.codehaus.cargo</pluginGroup>
</pluginGroups>

此文主要用於我的總結,可能有不少人看不明白,能夠聯繫我dyyweb@163.com,也能夠問度娘!
相關文章
相關標籤/搜索