maven私服設置與使用詳細


1.私服搭建環境

  在Linux系統中,我選擇比較方便下載安裝docker容器,具體安裝步驟可以根據Docker菜鳥教程安裝自己需要的鏡像。在這里我們先選擇

Docker 安裝 Nginx。這里就不做具體步驟講解。

 

2.私服設置

  私服搭建完成,我們輸入默認用戶名/密碼:admin/admin123進入首頁選擇設置

  

 在設置里面選擇方便后續管理。

 

  對應我創建的倉庫,在這里對倉庫類型做下簡要說明:

    hosted,本地倉庫,通常我們會部署自己的構件到這一類型的倉庫。比如公司的第二方庫。

  proxy,代理倉庫,它們被用來代理遠程的公共倉庫,如maven中央倉庫。

  group,倉庫組,用來合並多個hosted/proxy倉庫,當你的項目希望在多個repository使用資源時就不需要多次引用了,只需要引用一個group即可。

 對於倉庫的創建,wessence-releases和wessence-snapshots創建方式一樣,我們以wessence-releases為例

 

 

 

 

  創建倉庫分組,選擇maven2(group)類型進行創建。

私服基礎設置都這里設置完成,下面我們設置一下使用的settings和pom參數。下面是settings設置

  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 
  3 <!--
  4 Licensed to the Apache Software Foundation (ASF) under one
  5 or more contributor license agreements.  See the NOTICE file
  6 distributed with this work for additional information
  7 regarding copyright ownership.  The ASF licenses this file
  8 to you under the Apache License, Version 2.0 (the
  9 "License"); you may not use this file except in compliance
 10 with the License.  You may obtain a copy of the License at
 11 
 12     http://www.apache.org/licenses/LICENSE-2.0
 13 
 14 Unless required by applicable law or agreed to in writing,
 15 software distributed under the License is distributed on an
 16 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17 KIND, either express or implied.  See the License for the
 18 specific language governing permissions and limitations
 19 under the License.
 20 -->
 21 
 22 <!--
 23  | This is the configuration file for Maven. It can be specified at two levels:
 24  |
 25  |  1. User Level. This settings.xml file provides configuration for a single user,
 26  |                 and is normally provided in ${user.home}/.m2/settings.xml.
 27  |
 28  |                 NOTE: This location can be overridden with the CLI option:
 29  |
 30  |                 -s /path/to/user/settings.xml
 31  |
 32  |  2. Global Level. This settings.xml file provides configuration for all Maven
 33  |                 users on a machine (assuming they're all using the same Maven
 34  |                 installation). It's normally provided in
 35  |                 ${maven.home}/conf/settings.xml.
 36  |
 37  |                 NOTE: This location can be overridden with the CLI option:
 38  |
 39  |                 -gs /path/to/global/settings.xml
 40  |
 41  | The sections in this sample file are intended to give you a running start at
 42  | getting the most out of your Maven installation. Where appropriate, the default
 43  | values (values used when the setting is not specified) are provided.
 44  |
 45  |-->
 46 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
 47           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 48           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 49     <!-- localRepository
 50      | The path to the local repository maven will use to store artifacts.
 51      |
 52      | Default: ${user.home}/.m2/repository
 53     <localRepository>/path/to/local/repo</localRepository>
 54     -->
 55     <!--自定義本地倉庫路徑-->
 56   <localRepository>D:\dev\localwar\local</localRepository>
 57    <!-- interactiveMode
 58    | This will determine whether maven prompts you when it needs input. If set to false,
 59    | maven will use a sensible default value, perhaps based on some other setting, for
 60    | the parameter in question.
 61    |
 62    | Default: true
 63   <interactiveMode>true</interactiveMode>
 64   -->
 65 
 66   <!-- offline
 67    | Determines whether maven should attempt to connect to the network when executing a build.
 68    | This will have an effect on artifact downloads, artifact deployment, and others.
 69    |
 70    | Default: false
 71   <offline>false</offline>
 72   -->
 73 
 74   <!-- pluginGroups
 75    | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
 76    | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
 77    | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
 78    |-->
 79   <pluginGroups>
 80     <!-- pluginGroup
 81      | Specifies a further group identifier to use for plugin lookup.
 82     <pluginGroup>com.your.plugins</pluginGroup>
 83     -->
 84   </pluginGroups>
 85 
 86   <!-- proxies
 87    | This is a list of proxies which can be used on this machine to connect to the network.
 88    | Unless otherwise specified (by system property or command-line switch), the first proxy
 89    | specification in this list marked as active will be used.
 90    |-->
 91   <proxies>
 92     <!-- proxy
 93      | Specification for one proxy, to be used in connecting to the network.
 94      |
 95     <proxy>
 96       <id>optional</id>
 97       <active>true</active>
 98       <protocol>http</protocol>
 99       <username>proxyuser</username>
100       <password>proxypass</password>
101       <host>proxy.host.net</host>
102       <port>80</port>
103       <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
104     </proxy>
105     -->
106   </proxies>
107 
108   <!-- servers
109    | This is a list of authentication profiles, keyed by the server-id used within the system.
110    | Authentication profiles can be used whenever maven must make a connection to a remote server.
111    |-->
112   <servers>
113     <!-- server
114      | Specifies the authentication information to use when connecting to a particular server, identified by
115      | a unique name within the system (referred to by the 'id' attribute below).
116      |
117      | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
118      |       used together.
119      |
120      -->
121     <server>
122 <!--id 自己設置,但是必須與pom文件中引入倉庫的id一直,username,與password 是登錄私服的用戶名密碼-->
123       <id>releases</id>
124       <username>admin</username>
125       <password>admin123</password>
126     </server>
127     
128     <server>
129       <id>snapshots</id>
130       <username>admin</username>
131       <password>admin123</password>
132     </server>
133     
134     
135 
136     <!-- Another sample, using keys to authenticate.
137     <server>
138       <id>release</id>
139       <privateKey>/path/to/private/key</privateKey>
140       <passphrase>optional; leave empty if not used.</passphrase>
141     </server>
142     -->
143   </servers>
144 
145   <!-- mirrors
146    | This is a list of mirrors to be used in downloading artifacts from remote repositories.
147    |
148    | It works like this: a POM may declare a repository to use in resolving certain artifacts.
149    | However, this repository may have problems with heavy traffic at times, so people have mirrored
150    | it to several places.
151    |
152    | That repository definition will have a unique id, so we can create a mirror reference for that
153    | repository, to be used as an alternate download site. The mirror site will be the preferred
154    | server for that repository.
155    |-->
156   <mirrors>
157     <!-- mirror
158      | Specifies a repository mirror site to use instead of a given repository. The repository that
159      | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
160      | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
161      |
162     <mirror>
163       <id>mirrorId</id>
164       <mirrorOf>repositoryId</mirrorOf>
165       <name>Human Readable Name for this Mirror.</name>
166       <url>http://my.repository.com/repo/path</url>
167     </mirror>
168      -->
169   </mirrors>
170 
171   <!-- profiles
172    | This is a list of profiles which can be activated in a variety of ways, and which can modify
173    | the build process. Profiles provided in the settings.xml are intended to provide local machine-
174    | specific paths and repository locations which allow the build to work in the local environment.
175    |
176    | For example, if you have an integration testing plugin - like cactus - that needs to know where
177    | your Tomcat instance is installed, you can provide a variable here such that the variable is
178    | dereferenced during the build process to configure the cactus plugin.
179    |
180    | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
181    | section of this document (settings.xml) - will be discussed later. Another way essentially
182    | relies on the detection of a system property, either matching a particular value for the property,
183    | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
184    | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
185    | Finally, the list of active profiles can be specified directly from the command line.
186    |
187    | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
188    |       repositories, plugin repositories, and free-form properties to be used as configuration
189    |       variables for plugins in the POM.
190    |
191    |-->
192   <profiles>
193     <!-- profile
194      | Specifies a set of introductions to the build process, to be activated using one or more of the
195      | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
196      | or the command line, profiles have to have an ID that is unique.
197      |
198      | An encouraged best practice for profile identification is to use a consistent naming convention
199      | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
200      | This will make it more intuitive to understand what the set of introduced profiles is attempting
201      | to accomplish, particularly when you only have a list of profile id's for debug.
202      |
203      | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
204     <profile>
205       <id>jdk-1.4</id>
206 
207       <activation>
208         <jdk>1.4</jdk>
209       </activation>
210 
211       <repositories>
212         <repository>
213           <id>jdk14</id>
214           <name>Repository for JDK 1.4 builds</name>
215           <url>http://www.myhost.com/maven/jdk14</url>
216           <layout>default</layout>
217           <snapshotPolicy>always</snapshotPolicy>
218         </repository>
219       </repositories>
220     </profile>
221     -->
222 
223     <!--
224      | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
225      | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
226      | might hypothetically look like:
227      |
228      | ...
229      | <plugin>
230      |   <groupId>org.myco.myplugins</groupId>
231      |   <artifactId>myplugin</artifactId>
232      |
233      |   <configuration>
234      |     <tomcatLocation>${tomcatPath}</tomcatLocation>
235      |   </configuration>
236      | </plugin>
237      | ...
238      |
239      | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
240      |       anything, you could just leave off the <value/> inside the activation-property.
241      |
242     
243     <profile>
244       <id>env-dev</id>
245 
246       <activation>
247         <property>
248           <name>target-env</name>
249           <value>dev</value>
250         </property>
251       </activation>
252 
253     </profile>
254     -->
255     
256      <profile>
257 <!--激活倉庫的唯一標識,自己設置名稱 -->
258       <id>wessence_profile</id>
259       <repositories>
260         <!--包含需要連接到遠程倉庫的信息。id,name自己設置,不用和私服上的倉庫名稱致.在這個我們可以只設置我們的分組地址就可以了,具體引用到pom去設置 -->
261         <repository>
262           <!--遠程倉庫唯一標識 -->
263           <id>wessence-public</id>
264           <!--遠程倉庫名稱 -->
265           <name>wessence-public</name>
266           <!--如何處理遠程倉庫里發布版本的下載 -->
267           <releases>
268             <!--true或者false表示該倉庫是否為下載某種類型構件(發布版,快照版)開啟。 -->
269             <enabled>true</enabled>
270             <!--該元素指定更新發生的頻率。Maven會比較本地POM和遠程POM的時間戳。這里的選項是:always(一直),daily(默認,每日),interval:X(這里X是以分鍾為單位的時間間隔),或者never(從不)。 -->
271             <updatePolicy>never</updatePolicy>
272             <!--當Maven驗證構件校驗文件失敗時該怎么做-ignore(忽略),fail(失敗),或者warn(警告)。 -->
273             <checksumPolicy>warn</checksumPolicy>
274           </releases>
275           <!--如何處理遠程倉庫里快照版本的下載。有了releases和snapshots這兩組配置,POM就可以在每個單獨的倉庫中,為每種類型的構件采取不同的策略。例如,可能有人會決定只為開發目的開啟對快照版本下載的支持。參見repositories/repository/releases元素 -->
276           <snapshots>
277             <!--true或者false表示該倉庫是否為下載某種類型構件(發布版,快照版)開啟。 -->
278             <enabled>true</enabled>
279             <!--該元素指定更新發生的頻率。Maven會比較本地POM和遠程POM的時間戳。這里的選項是:always(一直),daily(默認,每日),interval:X(這里X是以分鍾為單位的時間間隔),或者never(從不)。 -->
280             <updatePolicy>always</updatePolicy>
281             <!--當Maven驗證構件校驗文件失敗時該怎么做-ignore(忽略),fail(失敗),或者warn(警告)。 -->
282             <checksumPolicy>warn</checksumPolicy>
283           </snapshots>
284           <!--遠程倉庫URL,按protocol://hostname/path形式 -->
285           <url>http://自己ip或者域名:8081/repository/wessence-public/</url>
286           <!--用於定位和排序構件的倉庫布局類型-可以是default(默認)或者legacy(遺留)。Maven 2為其倉庫提供了一個默認的布局;然而,Maven 1.x有一種不同的布局。我們可以使用該元素指定布局是default(默認)還是legacy(遺留)。 -->
287           <layout>default</layout>
288         </repository>
289       </repositories>
290       
291       <pluginRepositories>  
292         <pluginRepository>  
293           <id>wessence-group</id>  
294           <name>Maven China Mirror</name>  
295           <url>http://自己ip或者域名:8081/repository/wessence-public/</url>  
296           <releases>  
297             <enabled>true</enabled>  
298           </releases>  
299           <snapshots>  
300             <enabled>true</enabled>  
301           </snapshots>      
302         </pluginRepository>  
303       </pluginRepositories> 
304   
305     </profile>
306   </profiles>
307  
308 
309 
310   <!-- activeProfiles
311    | List of profiles that are active for all builds.
312    -->
313   <activeProfiles>
314     <activeProfile>wessence_profile</activeProfile>
315   </activeProfiles>
316 
317 </settings>

 

pom設置

<distributionManagement>
        <repository>
<!--id 必須與settings里面的server id一致-->
            <id>releases</id>
            <name>corp nexus-releases</name>
            <url>http://自己ip或者域名:8081/repository/wessence-releases/</url>
        </repository>
        <snapshotRepository>
<!--id 必須與settings里面的server id一致-->
            <id>snapshots</id>
            <name>corp nexus-snapshot</name>
            <url>http://自己ip或者域名:8081/repository/wessence-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

以上文檔有不對大家的請指正。


免責聲明!

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



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