---
title: "Maven2如何使用私有Repository"
author: "maomaode"
date: "2007-07-24T09:03:33Z"
tags: []
slug: "maven2-repository"
draft: false
lastmod: "2007-07-24T09:03:33Z"
id: 106985
url: "https://maomaode.iteye.com/blog/106985"
---

&lt;link rel="stylesheet" type="text/css" href="http://maomaode.bokeland.com/plugins/plogeshi/styles/plogeshi.css"&gt;早在Celtix， 和之前的CXF里，我们在maven里定义私有的Repository， 因为我们所依赖的Jar， 还没发布到Maven的Repository里，或者，那个项目压根儿就不用maven来构建。  
配置其实很简单:  
修改pom.xml, 加入下面这个片段，

```
    <repositories>
        <repository>
            <id>private-repo</id>
            <name>private repo</name>
            <url>file:${basedir}/${topDirectoryLocation}/maven_repo</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
 
```

并定义这个变量:

```
    <properties>
        <topDirectoryLocation>.</topDirectoryLocation>
     </properties>
 
```

在maven\_repo里就可以存放你得jar， 目录结构应该和.m2/repository一致  
如果jar还没有pom， 则需要写一个pom， 例如下面就是一个例子:  
&lt;link rel="stylesheet" type="text/css" href="http://maomaode.bokeland.com/plugins/plogeshi/styles/plogeshi.css"&gt;

```
 
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>htmlunit</groupId>
  <artifactId>htmlunit-patch</artifactId>
  <version>1.10</version>
  <name>htmlunit patch jar</name>
  <url>http://htmlunit.sf.net</url>
  <distributionManagement>
    <downloadUrl>http://htmlunit.sf.net</downloadUrl>
  </distributionManagement>
  <dependencies>
    <dependency>
      <groupId>htmlunit</groupId>
      <artifactId>htmlunit</artifactId>
      <version>1.10</version>
    </dependency>
  </dependencies>
</project>
 
```

此外你还需要为jar， 和pom ，做出sha1文件，可以使用工具sha1sum， windows上需要安装cygwin才可以使用这个工具。

Maven 在无法找到这个artifact时候才会去private repository里找，　并安装到.m2目录下。之后就不会从这个目录取了  
所以他和运行时classpath没有关系，Maven不会把private repo里的patch jar放在classpath的最前面，maven 本身并不能让我们来改变classpath, 所以如果是你自己的patch jar, 你无法通过这个方法来得到解决，唯一的解决方法就是把你的patch 和原jar合并为一个。或者是用[fant](http://code.google.com/p/fant/)来解决您的问题。