Published on

java库项目-jar打包并上传私有库,pom.xml引入

Authors

https://www.cnblogs.com/zhangwuji/p/10040834.html 三种方式

环境

maven 包管理

maven package打包

使用 maven package 打包,打包完jar后,在target目录下。 其他工程引入该包,会找不到该包依赖的其他文件而报错。

  • 带依赖打包 在pom中加入插件
    打包并打包所有依赖    
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>3.2.0</version>
          <configuration>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
          </configuration>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

这也打的jar包可以直接给别人使用,但是依赖包的版本被固定了,不够灵活。

maven install 打包,并安装到本地maven repository

打包后,在其他工程的pom.xml加入,会自动查找jar的pom依赖,并解决。

    <dependency>
      <groupId>com.sanhe</groupId>
      <artifactId>fabricsdk</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

跟引入中央库的操作一致。

私有仓库的使用

1. 创建一个新的repository
2. 把创建的hosted移动进来
3. 上传jar包
记得选POM,这样引入后才会自动解决依赖问题
3. 配置maven setting.xml文件
 <mirror>
      <id>public</id>
      <mirrorOf>*</mirrorOf>
      <name>Public Repositories</name>
      <url>http://xxxx</url>
 </mirror>

这样已经可以引入我们需要的包了,但这种方式并不完美。 公司的私服public已经做了central仓库的代理,所以无脑的这样写是可以用的,但是如果我想用aliyun的central镜像,这样就写就不行了。或者说public没有代理central仓库,引入一些central库的时候就会出问题。

下面这个是 maven 引入顺序,默认就是去central里面找,所以mirrorOf * 写成 central也可以。 local_repo > settings_profile_repo > pom_profile_repo > pom_repositories > settings_mirror > central https://blog.csdn.net/qq_24393347/article/details/100763032 maven引入顺序链接

不要使用<mirrorOf>*<mirrorOf>

使用 * 之后,maven所有的仓库,依赖都在这一个url里面找。找不到就报错,不会去profile里面设置的找 settings.xml 设置私有源

    <mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>阿里云公共仓库</name> 之前阿里这里我也写了*,改为centrl
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>

    <profile>
      <id>blockchaindev</id>
      <repositories>
        <repository>
          <id>标识id随便写</id>
          <name>名称随便写 Repositories</name>
          <url>http://xx.xxx.xx.xxx/nexus/content/repositories/3hblockchain/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
        </repository>
      </repositories>
    </profile>

开启配置
  <activeProfiles>
    <activeProfile>blockchaindev</activeProfile>
  </activeProfiles>

在pom里面设置也可以。