maven传递性依赖与NoSuchMethodError

简介:

    对于maven, 很多人看似很熟悉,其实貌似根本不了解,本文旨在解决某些方面的疑惑。

       当我在service模块引入公司某业务的一个接口,由于该接口依赖了公司写的各种东西和spring等第三方依赖,所以我一下子啥都不用显示依赖了,这叫传递性。而dependencyManagement的作用在于仍然让子模块显示写依赖,但依赖的版本和设置不用写了,简化了。关于jar包的传递依赖,你可以在idea的maven窗口中选定子模块右键show dependencies。

       在idea环境中,有时你不得不执行reimport和clean,才能看到jar包引入。另外,请注意你写的version是仓库中确实存在的。

       如果公司没有maven私服,完全依赖apache maven库,也许是够用的,碰到其他公司的接口需要你引入他们的jar包时,可以把jar包放在jvm/ext/lib下。

       值得一提的是,在模块组织方面,你完全可以把紧密相关的几个系统的放在一起,各个系统的web模块公用一套common, do及dao模块,而不必为每个系统打开一个idea窗口。

       pom里已经引入dependency,但很多时候我们需要reimport一下甚至很多下才能看到jar包被真正引入,如果module被ignore了,那么也很可能引入不了jar包。注意,只要dependency没变红,就是OK的。

       本地install时,请把当前模块依赖的模块先install一下。

       本地测试时,idea开启了自动indexing时,很有可能远程的包会覆盖本地的包,导致本地要重新install。

       maven的传递性依赖会导致间接进入一些包而与显示引入的发生冲突,例如,某依赖间接引入了spring2.5.6,模块里显示依赖的是spring3.1.1,spring2.5.6里ThreadPoolTaskExecutor没有submit(Runnable)方法,spring3.1.1里有,程序运行时极有可能加载spring2.5.6的ThreadPoolTaskExecutor.class而发生java.lang.NoSuchMethodError。

 

maven其他注意点:

  由于历史原因,maven的核心插件之一——compiler插件默认只支持编译Java 1.3,因此需要配置该插件使其支持Java 5.

典型的maven配置示例:

 父pom

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <modelVersion>4.0.0</modelVersion>  
  6.   
  7.     <groupId>com.ixhong</groupId>  
  8.     <artifactId>ixhong</artifactId>  
  9.     <packaging>pom</packaging>  
  10.     <version>1.0-SNAPSHOT</version>  
  11.     <modules>  
  12.         <module>ixhong-common</module>  
  13.         <module>ixhong-admin-web</module>  
  14.         <module>ixhong-domain</module>  
  15.         <module>ixhong-dao</module>  
  16.         <module>ixhong-manager</module>  
  17.         <module>ixhong-fs-web</module>  
  18.         <module>ixhong-support-web</module>  
  19.         <module>ixhong-lender-web</module>  
  20.         <module>ixhong-base</module>  
  21.         <module>ixhong-tomcat-web</module>  
  22.     </modules>  
  23.   
  24.     <properties>  
  25.         <spring.version>3.2.11.RELEASE</spring.version>  
  26.         <ibatis.version>3.2.7</ibatis.version>  
  27.         <rootdir>${basedir}</rootdir>  
  28.     </properties>  
  29.     <dependencies>  
  30.         <dependency>  
  31.             <groupId>commons-lang</groupId>  
  32.             <artifactId>commons-lang</artifactId>  
  33.             <version>2.6</version>  
  34.         </dependency>  
  35.         <dependency>  
  36.             <groupId>commons-collections</groupId>  
  37.             <artifactId>commons-collections</artifactId>  
  38.             <version>3.2.1</version>  
  39.         </dependency>  
  40.   
  41.         <dependency>  
  42.             <groupId>commons-codec</groupId>  
  43.             <artifactId>commons-codec</artifactId>  
  44.             <version>1.10</version>  
  45.         </dependency>  
  46.         <!-- -->  
  47.         <dependency>  
  48.             <groupId>log4j</groupId>  
  49.             <artifactId>log4j</artifactId>  
  50.             <version>1.2.12</version>  
  51.         </dependency>  
  52.     </dependencies>  
  53.     <dependencyManagement>  
  54.         <dependencies>  
  55.             <dependency>  
  56.                 <groupId>javax.servlet</groupId>  
  57.                 <artifactId>javax.servlet-api</artifactId>  
  58.                 <version>3.0.1</version>  
  59.                 <scope>provided</scope>  
  60.             </dependency>  
  61.             <dependency>  
  62.                 <groupId>redis.clients</groupId>  
  63.                 <artifactId>jedis</artifactId>  
  64.                 <version>2.8.0</version>  
  65.             </dependency>  
  66.             <dependency>  
  67.                 <groupId>org.apache.struts</groupId>  
  68.                 <artifactId>struts2-core</artifactId>  
  69.                 <version>2.2.3.1</version>  
  70.             </dependency>  
  71.             <dependency>  
  72.                 <groupId>org.apache.tomcat</groupId>  
  73.                 <artifactId>tomcat-catalina</artifactId>  
  74.                 <version>7.0.57</version>  
  75.                 <scope>provided</scope>  
  76.             </dependency>  
  77.             <dependency>  
  78.                 <groupId>org.apache.tomcat</groupId>  
  79.                 <artifactId>tomcat-coyote</artifactId>  
  80.                 <version>7.0.57</version>  
  81.                 <scope>provided</scope>  
  82.             </dependency>  
  83.         </dependencies>  
  84.     </dependencyManagement>  
  85.     <!--<scm>  
  86.         <connection>scm:git:git@git.qwexyz.com:qwexyz/qwexyz.git</connection>  
  87.         <url>scm:git:git@git.qwexyz.com:qwexyz/qwexyz.git</url>  
  88.         <developerConnection>scm:git:git@git.qwexyz.com:qwexyz/qwexyz.git</developerConnection>  
  89.     </scm>-->  
  90.     <build>  
  91.         <plugins>  
  92.             <plugin>  
  93.                 <artifactId>maven-compiler-plugin</artifactId>  
  94.                 <version>3.1</version>  
  95.                 <configuration>  
  96.                     <source>1.6</source>  
  97.                     <target>1.6</target>  
  98.                     <encoding>UTF-8</encoding>  
  99.                 </configuration>  
  100.             </plugin>  
  101.             <plugin>  
  102.                 <artifactId>maven-resources-plugin</artifactId>  
  103.                 <version>2.6</version>  
  104.                 <configuration>  
  105.                     <encoding>UTF-8</encoding>  
  106.                 </configuration>  
  107.             </plugin>  
  108.             <plugin>  
  109.                 <groupId>org.apache.maven.plugins</groupId>  
  110.                 <artifactId>maven-release-plugin</artifactId>  
  111.                 <version>2.1</version>  
  112.             </plugin>  
  113.         </plugins>  
  114.     </build>  
  115.   
  116. </project>  

  web

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <parent>  
  6.         <artifactId>ixhong</artifactId>  
  7.         <groupId>com.ixhong</groupId>  
  8.         <version>1.0-SNAPSHOT</version>  
  9.     </parent>  
  10.     <modelVersion>4.0.0</modelVersion>  
  11.   
  12.     <artifactId>ixhong-lender-web</artifactId>  
  13.   
  14.     <packaging>war</packaging>  
  15.     <name>ixhong-user-web</name>  
  16.     <url>http://maven.apache.org</url>  
  17.     <!-- 依赖包 -->  
  18.     <dependencies>  
  19.   
  20.         <dependency>  
  21.             <groupId>com.ixhong</groupId>  
  22.             <artifactId>ixhong-manager</artifactId>  
  23.             <version>1.0-SNAPSHOT</version>  
  24.         </dependency>  
  25.         <dependency>  
  26.             <groupId>com.ixhong</groupId>  
  27.             <artifactId>ixhong-common</artifactId>  
  28.             <version>1.0-SNAPSHOT</version>  
  29.         </dependency>  
  30.         <dependency>  
  31.             <groupId>org.mybatis</groupId>  
  32.             <artifactId>mybatis-spring</artifactId>  
  33.             <version>1.2.2</version>  
  34.         </dependency>  
  35.         <dependency>  
  36.             <groupId>org.springframework</groupId>  
  37.             <artifactId>spring-context</artifactId>  
  38.             <version>${spring.version}</version>  
  39.         </dependency>  
  40.         <dependency>  
  41.             <groupId>org.springframework</groupId>  
  42.             <artifactId>spring-context-support</artifactId>  
  43.             <version>${spring.version}</version>  
  44.         </dependency>  
  45.         <dependency>  
  46.             <groupId>org.springframework</groupId>  
  47.             <artifactId>spring-webmvc</artifactId>  
  48.             <version>${spring.version}</version>  
  49.         </dependency>  
  50.         <dependency>  
  51.             <groupId>org.springframework</groupId>  
  52.             <artifactId>spring-jdbc</artifactId>  
  53.             <version>${spring.version}</version>  
  54.         </dependency>  
  55.         <dependency>  
  56.             <groupId>mysql</groupId>  
  57.             <artifactId>mysql-connector-java</artifactId>  
  58.             <version>5.1.32</version>  
  59.         </dependency>  
  60.   
  61.         <dependency>  
  62.             <groupId>org.apache.velocity</groupId>  
  63.             <artifactId>velocity</artifactId>  
  64.             <version>1.7</version>  
  65.         </dependency>  
  66.         <dependency>  
  67.             <groupId>javax.servlet</groupId>  
  68.             <artifactId>javax.servlet-api</artifactId>  
  69.             <version>3.0.1</version>  
  70.             <scope>provided</scope>  
  71.         </dependency>  
  72.   
  73.   
  74.         <dependency>  
  75.             <groupId>commons-dbcp</groupId>  
  76.             <artifactId>commons-dbcp</artifactId>  
  77.             <version>1.4</version>  
  78.         </dependency>  
  79.         <dependency>  
  80.             <groupId>commons-fileupload</groupId>  
  81.             <artifactId>commons-fileupload</artifactId>  
  82.             <version>1.3.1</version>  
  83.         </dependency>  
  84.         <dependency>  
  85.             <groupId>org.apache.velocity</groupId>  
  86.             <artifactId>velocity-tools</artifactId>  
  87.             <version>2.0</version>  
  88.             <exclusions>  
  89.                 <exclusion>  
  90.                     <groupId>org.apache.struts</groupId>  
  91.                     <artifactId>*</artifactId>  
  92.                 </exclusion>  
  93.             </exclusions>  
  94.         </dependency>  
  95.   
  96.         <dependency>  
  97.             <groupId>org.apache.activemq</groupId>  
  98.             <artifactId>activemq-client</artifactId>  
  99.             <version>5.11.1</version>  
  100.         </dependency>  
  101.         <dependency>  
  102.             <groupId>org.springframework</groupId>  
  103.             <artifactId>spring-jms</artifactId>  
  104.             <version>${spring.version}</version>  
  105.         </dependency>  
  106.   
  107.   
  108.   
  109.         <dependency>  
  110.             <groupId>org.springframework</groupId>  
  111.             <artifactId>spring-test</artifactId>  
  112.             <version>${spring.version}</version>  
  113.             <scope>test</scope>  
  114.         </dependency>  
  115.         <dependency>  
  116.             <groupId>junit</groupId>  
  117.             <artifactId>junit</artifactId>  
  118.             <version>4.8.2</version>  
  119.             <scope>test</scope>  
  120.         </dependency>  
  121.   
  122.     </dependencies>  
  123.     <build>  
  124.         <finalName>ROOT</finalName>  
  125.         <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>  
  126.         <resources>  
  127.             <resource>  
  128.                 <directory>src/main/resources</directory>  
  129.                 <filtering>true</filtering>  
  130.             </resource>  
  131.         </resources>  
  132.     </build>  
  133.   
  134.     <profiles>  
  135.         <profile>  
  136.             <id>development</id>  
  137.             <activation>  
  138.                 <activeByDefault>true</activeByDefault>  
  139.             </activation>  
  140.             <build>  
  141.                 <filters>  
  142.                     <filter>../profile/development.properties</filter>  
  143.                 </filters>  
  144.             </build>  
  145.         </profile>  
  146.         <profile>  
  147.             <id>test</id>  
  148.             <build>  
  149.                 <filters>  
  150.                     <filter>../profile/test.properties</filter>  
  151.                 </filters>  
  152.             </build>  
  153.         </profile>  
  154.         <profile>  
  155.             <id>production</id>  
  156.             <build>  
  157.                 <filters>  
  158.                     <filter>../profile/production.properties</filter>  
  159.                 </filters>  
  160.             </build>  
  161.         </profile>  
  162.     </profiles>  
  163.   
  164.   
  165. </project>  

  dao

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <parent>  
  6.         <artifactId>ixhong</artifactId>  
  7.         <groupId>com.ixhong</groupId>  
  8.         <version>1.0-SNAPSHOT</version>  
  9.     </parent>  
  10.     <modelVersion>4.0.0</modelVersion>  
  11.   
  12.     <artifactId>ixhong-dao</artifactId>  
  13.   
  14.     <packaging>jar</packaging>  
  15.     <name>ixhong-dao</name>  
  16.     <url>http://maven.apache.org</url>  
  17.     <build>  
  18.         <finalName>ixhong-dao</finalName>  
  19.         <resources>  
  20.             <resource>  
  21.                 <directory>src/main/resources</directory>  
  22.                 <filtering>true</filtering>  
  23.             </resource>  
  24.         </resources>  
  25.     </build>  
  26.     <dependencies>  
  27.         <dependency>  
  28.             <groupId>com.ixhong</groupId>  
  29.             <artifactId>ixhong-domain</artifactId>  
  30.             <version>1.0-SNAPSHOT</version>  
  31.         </dependency>  
  32.         <dependency>  
  33.             <groupId>org.mybatis</groupId>  
  34.             <artifactId>mybatis</artifactId>  
  35.             <version>${ibatis.version}</version>  
  36.         </dependency>  
  37.   
  38.     </dependencies>  
  39.   
  40.   
  41. </project>  

  domain

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <parent>  
  6.         <artifactId>ixhong</artifactId>  
  7.         <groupId>com.ixhong</groupId>  
  8.         <version>1.0-SNAPSHOT</version>  
  9.     </parent>  
  10.     <modelVersion>4.0.0</modelVersion>  
  11.   
  12.     <artifactId>ixhong-domain</artifactId>  
  13.   
  14.     <packaging>jar</packaging>  
  15.     <name>ixhong-domain</name>  
  16.     <url>http://maven.apache.org</url>  
  17.     <build>  
  18.         <finalName>ixhong-domain</finalName>  
  19.     </build>  
  20.     <dependencies>  
  21.         <dependency>  
  22.             <groupId>net.sf.json-lib</groupId>  
  23.             <artifactId>json-lib</artifactId>  
  24.             <version>2.4</version>  
  25.             <classifier>jdk15</classifier>  
  26.         </dependency>  
  27.     </dependencies>  
  28.   
  29.   
  30. </project>  

 

maven的核心仅仅定义了抽象的生命周期,具体的任务是交由插件完成的,插件以独立的构建形式存在,因此,maven核心的分发包只有不到3MB的大小,Maven会在需要的时候下载并使用插件。一个插件可能会对应多个目标,一个目标就是一个功能,如maven-dependency-plugin有十多个目标,dependency:analyze,dependency:tree,dependency:list等。

可以使用jetty-maven-plugin启动web应用进行测试。

 

配置文件

settings.xml中的profile是pom.xml中的profile的简洁形式。它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素。profile元素仅包含这四个元素是因为他们涉及到整个的构建系统,而不是个别的POM配置。

如果settings中的profile被激活,那么它的值将重载POM或者profiles.xml中的任何相等ID的profiles。

激活(activation)

activations是profile的关键,就像POM中的profiles,profile的能力在于它在特定情况下可以修改一些值。而这些情况是通过activation来指定的。

 

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.0.0"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  4.   <profiles>  
  5.     <profile>  
  6.       <repositories>  
  7.         <repository>  
  8.           <snapshots>  
  9.             <enabled>false</enabled>  
  10.           </snapshots>  
  11.           <id>central</id>  
  12.           <name>libs-releases</name>  
  13.           <url>http://maven.whatsmars.com/libs-releases</url>  
  14.         </repository>  
  15.         <repository>  
  16.           <snapshots />  
  17.           <id>snapshots</id>  
  18.           <name>libs-snapshots</name>  
  19.           <url>http://maven.whatsmars.com/libs-snapshots</url>  
  20.         </repository>  
  21.       </repositories>  
  22.       <pluginRepositories>  
  23.         <pluginRepository>  
  24.           <snapshots>  
  25.             <enabled>false</enabled>  
  26.           </snapshots>  
  27.           <id>central</id>  
  28.           <name>plugins-releases</name>  
  29.           <url>http://maven.whatsmars.com/plugins-releases</url>  
  30.         </pluginRepository>  
  31.         <pluginRepository>  
  32.           <snapshots />  
  33.           <id>snapshots</id>  
  34.           <name>plugins-snapshots</name>  
  35.           <url>http://maven.whatsmars.com/plugins-snapshots</url>  
  36.         </pluginRepository>  
  37.       </pluginRepositories>  
  38.       <id>artifactory</id>  
  39.     </profile>  
  40.   </profiles>  
  41.   <activeProfiles>  
  42.     <activeProfile>artifactory</activeProfile>  
  43.   </activeProfiles>  
  44. </settings>  

 mvn package-DskipTests  打包并且跳过测试

mvn clean install-Pdev  激活dev下的profile,这样properties中的配置就会替换成实际值

 

maven可继承的pom元素

Xml代码   收藏代码
  1. groupId :项目组 ID ,项目坐标的核心元素;  
  2. version :项目版本,项目坐标的核心元素;  
  3. description :项目的描述信息;  
  4. organization :项目的组织信息;  
  5. inceptionYear :项目的创始年份;  
  6. url :项目的 url 地址  
  7. develoers :项目的开发者信息;  
  8. contributors :项目的贡献者信息;  
  9. distributionManagerment :项目的部署信息;  
  10. issueManagement :缺陷跟踪系统信息;  
  11. ciManagement :项目的持续继承信息;  
  12. scm :项目的版本控制信息;  
  13. mailingListserv :项目的邮件列表信息;  
  14. properties :自定义的 Maven 属性;  
  15. dependencies :项目的依赖配置;  
  16. dependencyManagement :醒目的依赖管理配置;  
  17. repositories :项目的仓库配置;  
  18. build :包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等;  
  19. reporting :包括项目的报告输出目录配置、报告插件配置等。  


原文链接:[http://wely.iteye.com/blog/2270414 ]
相关文章
|
3月前
|
存储 Java 测试技术
Maven依赖范围
Maven依赖范围
97 0
|
4月前
|
Java Maven
maven 依赖剔除处理
maven 依赖剔除处理
24 0
|
12天前
|
Java 应用服务中间件 Maven
使用IDEA搭建SpringMVC环境,Maven导入了依赖,但是运行报错 java.lang.ClassNotFoundException
使用IDEA搭建SpringMVC环境,Maven导入了依赖,但是运行报错 java.lang.ClassNotFoundException
11 1
|
15天前
|
Java Maven
SpringMVC常用Maven POM.xml依赖包片段
SpringMVC常用Maven POM.xml依赖包片段
|
1月前
|
Java Maven
Maven【3】( 依赖的范围,传递性和依赖的排除)(命令行操作)
Maven【3】( 依赖的范围,传递性和依赖的排除)(命令行操作)
21 0
|
1月前
|
Java Maven
Maven【2】( Web 工程依赖 Java 工程)(命令行操作)
Maven【2】( Web 工程依赖 Java 工程)(命令行操作)
20 0
|
1月前
|
Java Maven
idea的Maven依赖问题
idea的Maven依赖问题
|
2月前
|
缓存 Java Maven
Maven找不到依赖终极解决方案
Maven找不到依赖终极解决方案
166 0
|
2月前
|
缓存 Java Maven
【Maven】工程依赖下载失败错误解决
【Maven】工程依赖下载失败错误解决
56 0
|
3月前
|
Java Maven
IDEA中maven工程下pom.xml的某些依赖按ctrl点不进去
IDEA中maven工程下pom.xml的某些依赖按ctrl点不进去
31 0

推荐镜像

更多