Maven配置——增删(自动)更新依赖

<span style="font-family: 'Microsoft YaHei', 微软雅黑, Arial, 'Lucida Grande', Tahoma, sans-serif; background-color: rgb(240, 240, 240);">规则是在要导入的包中加一个去除原有依赖的标签,标签内写想去除的包的id和版本。</span>
<dependency>
			<groupId>com.sun.jersey.contribs</groupId>
			<artifactId>jersey-spring</artifactId>
			<version>1.18.1</version>
			<!-- 去除自带的spring版本 -->
            		<exclusions>
                		<exclusion>
                    			<groupId>org.springframework</groupId>
                    			<artifactId>spring-aop</artifactId>
                		</exclusion>
                		<exclusion>
                    			<groupId>org.springframework</groupId>
                    			<artifactId>spring-context</artifactId>
                		</exclusion>
                		<exclusion>
                    			<groupId>org.springframework</groupId>
                    			<artifactId>spring-beans</artifactId>
                		</exclusion>
                		<exclusion>
                    			<groupId>org.springframework</groupId>
                    			<artifactId>spring-core</artifactId>
                		</exclusion>
                		<exclusion>
                    			<artifactId>spring-web</artifactId>
                    			<groupId>org.springframework</groupId>
                		</exclusion>
            		</exclusions>
		</dependency>
 
 
--------------------------------------------------------------------------------------------------------------------
<h4 style="font-size: 1.15em; line-height: 1.3em; margin: 0px 0px 0.25em; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; background-color: rgb(239, 239, 239);">依赖关系</h4><h6 style="font-size: 1em; line-height: 1.3em; margin: 0px 0px 0.25em; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; background-color: rgb(239, 239, 239);"><a target=_blank name="%E8%92%8B%E5%BB%BA%E6%B3%A2%E7%9A%84maven%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E9%97%AE%EF%BC%9A%E5%A6%82%E4%BD%95%E5%A2%9E%E5%8A%A0%E5%88%A0%E9%99%A4%E4%B8%80%E4%B8%AA%E4%BE%9D%E8%B5%96%E5%85%B3%E7%B3%BB%EF%BC%9F" style="color: rgb(0, 102, 153); text-decoration: underline;"></a>问:如何增加删除一个依赖关系?</h6><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px; background-color: rgb(239, 239, 239);">答:直接在pom文件中加入一个dependency节点,如果要删除依赖,把对应的dependency节点删除即可。</p><h6 style="font-size: 1em; line-height: 1.3em; margin: 0px 0px 0.25em; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; background-color: rgb(239, 239, 239);"><a target=_blank name="%E8%92%8B%E5%BB%BA%E6%B3%A2%E7%9A%84maven%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%EF%BC%9A%E5%A6%82%E4%BD%95%E5%B1%8F%E8%94%BD%E4%B8%80%E4%B8%AA%E4%BE%9D%E8%B5%96%E5%85%B3%E7%B3%BB%EF%BC%9F%E6%AF%94%E5%A6%82%E9%A1%B9%E7%9B%AE%E4%B8%AD%E4%BD%BF%E7%94%A8%E7%9A%84libA%E4%BE%9D%E8%B5%96%E6%9F%90%E4%B8%AA%E5%BA%93%E7%9A%841.0%E7%89%88%EF%BC%8ClibB%E4%BB%A5%E6%9D%A5%E6%9F%90%E4%B8%AA%E5%BA%93%E7%9A%84..." style="color: rgb(0, 102, 153); text-decoration: underline;"></a>问:如何屏蔽一个依赖关系?比如项目中使用的libA依赖某个库的1.0版,libB以来某个库的2.0版,现在想统一使用2.0版,如何去掉1.0版的依赖?</h6><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px; background-color: rgb(239, 239, 239);">答:设置exclusion即可。</p><div class="dp-highlighter" id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: rgb(239, 239, 239); width: 766.296875px; overflow: auto; margin-left: 9px; padding: 1px; word-break: break-all; word-wrap: break-word; line-height: 25.1875px;"><div class="bar"><div class="tools" style="padding: 3px; margin: 0px; font-weight: bold;">Xml代码  <a target=_blank title="收藏这段代码" style="color: rgb(0, 102, 153); text-decoration: underline;"><img class="star" src="http://www.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px;" /></a></div></div><ol start="1" class="dp-xml" style="font-size: 1em; line-height: 1.4em; margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(43, 145, 175);"><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;"><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">dependency</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">groupId</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>org.hibernate<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">groupId</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">artifactId</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>hibernate<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">artifactId</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">version</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>3.2.5.ga<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">version</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">exclusions</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">      <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">exclusion</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">groupId</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>javax.transaction<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">groupId</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">        <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"><</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">artifactId</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>jta<span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">artifactId</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">      <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">exclusion</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">    <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">exclusions</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); background-color: rgb(250, 250, 250); line-height: 18px;"><span style="color: black;">  <span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;"></</span><span class="tag-name" style="color: rgb(0, 102, 153); font-weight: bold;">dependency</span><span class="tag" style="color: rgb(0, 102, 153); font-weight: bold;">></span>  </span></li></ol></div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px; background-color: rgb(239, 239, 239);"> </span><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px; background-color: rgb(239, 239, 239);"> </p><div class="code panel" style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px; background-color: rgb(239, 239, 239); border-width: 1px;"><div class="codeContent panelContent"></div></div><h6 style="font-size: 1em; line-height: 1.3em; margin: 0px 0px 0.25em; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; background-color: rgb(239, 239, 239);"><a target=_blank name="%E8%92%8B%E5%BB%BA%E6%B3%A2%E7%9A%84maven%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%EF%BC%9A%E6%88%91%E6%9C%89%E4%B8%80%E4%BA%9Bjar%E6%96%87%E4%BB%B6%E8%A6%81%E4%BE%9D%E8%B5%96%EF%BC%8C%E4%BD%86%E6%98%AF%E6%88%91%E5%8F%88%E4%B8%8D%E6%83%B3%E6%8A%8A%E8%BF%99%E4%BA%9Bjar%E5%8E%BBinstall%E5%88%B0mvn%E7%9A%84reposi..." style="color: rgb(0, 102, 153); text-decoration: underline;"></a>问:我有一些jar文件要依赖,但是我又不想把这些jar去install到mvn的repository中去,怎么做配置?</h6><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px; background-color: rgb(239, 239, 239);">答:加入一个特殊的依赖关系,使用system类型,如下:</p><div class="code panel" style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.1875px; background-color: rgb(239, 239, 239); border-width: 1px;"><div class="codeContent panelContent"><pre class="code-xml" style="font-size: 1em;"><dependency>
  		<groupId>com.abc</groupId>
  		<artifactId>my-tools</artifactId>
  		<version>2.5.0</version>
  		<type>jar</type>
	    <scope>system</scope>
           <systemPath>${basedir}/lib/mylib1.jar</systemPath>
	</dependency>
 

但是要记住,发布的时候不会复制这个jar。需要手工配置,而且其他project依赖这个project的时候,会报告警告。如果没有特殊要求,建议直接注册发布到repository。

问:在eclipse环境中同时使用maven builder和eclipse builder,并且设置项目依赖关系之后,为什么编译会出现artifact找不到错误,但是直接使用命令行mvn构建则一切正常?

答:在project属性中去掉java build path中对其他 project 的依赖关系,直接在pom中设置依赖关系即可

<!-- 依赖的其他项目 -->
  	<dependency>
		<groupId>com.abc.project1</groupId>
		<artifactId>abc-project1-common</artifactId>
		<version>${project.version}</version>
  	</dependency>
 

另外,保证没有其他错误。

问:我想让输出的jar包自动包含所有的依赖

答:使用 assembly 插件即可。

<plugin>
       <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
--------------------------------------------------------------------------------------------------------------
 
 
<pre class="prettyprint xml" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 1.5em; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(246, 246, 246); border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto;"><span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"><</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">dependency</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span>
          <span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"><</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">groupId</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span>org.seleniumhq.selenium<span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"></</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">groupId</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span>
          <span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"><</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">artifactId</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span>selenium-java<span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"></</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">artifactId</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span>
          <span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"><</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">version</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span>[2.40.0,)<span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"></</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">version</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span>
      <span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"></</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">dependency</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span>

以上注意version里的[2.40.0,)表示取2.40.0以上最新版本。当我这样写version之后,我的selenium框架里的jar包就会自动升级了,现在他们自己变成2.41.0版本了。

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

 maven中手动去更新jar包,是一件比较繁琐麻烦的事情,使用Range Dependency则可以省去这一步骤。

  部分jar包可能会自动升级到beat版本!

  官方说明文档:Dependency Version Ranges

Range Meaning
 (,1.0]  version ≤ 1.0
 1.0  固定1.0版本
 [1.0]  
 [1.2,1.3]  1.2 ≤ version ≤ 1.3
 [1.0,2.0)  1.0 ≤ version ≤ 2.0
 [1.5,)  version ≥ 1.5
 (,1.0],[1.2,)  x ≤ 1.0 || x ≥ 1.2 (区间用英文逗号隔开)
 (,1.1),(1.1,)  version ≠ 1.0

  netbeans中添加依赖关系时手动输入range

   Maven配置——增删(自动)更新依赖_第1张图片

  pom.xml中生成的代码

?
1
2
3
4
5
< dependency >
     < groupId >org.apache.velocity</ groupId >
     < artifactId >velocity</ artifactId >
     < version >(1.5,)</ version >
</ dependency >

  依赖关系中出现:

  

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



 

你可能感兴趣的:(maven)