maven传递性依赖与NoSuchMethodError

    xiaoxiao2022-07-15  163

        对于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代码   <?xml version="1.0" encoding="UTF-8"?>  <project xmlns="http://maven.apache.org/POM/4.0.0"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">      <modelVersion>4.0.0</modelVersion>        <groupId>com.ixhong</groupId>      <artifactId>ixhong</artifactId>      <packaging>pom</packaging>      <version>1.0-SNAPSHOT</version>      <modules>          <module>ixhong-common</module>          <module>ixhong-admin-web</module>          <module>ixhong-domain</module>          <module>ixhong-dao</module>          <module>ixhong-manager</module>          <module>ixhong-fs-web</module>          <module>ixhong-support-web</module>          <module>ixhong-lender-web</module>          <module>ixhong-base</module>          <module>ixhong-tomcat-web</module>      </modules>        <properties>          <spring.version>3.2.11.RELEASE</spring.version>          <ibatis.version>3.2.7</ibatis.version>          <rootdir>${basedir}</rootdir>      </properties>      <dependencies>          <dependency>              <groupId>commons-lang</groupId>              <artifactId>commons-lang</artifactId>              <version>2.6</version>          </dependency>          <dependency>              <groupId>commons-collections</groupId>              <artifactId>commons-collections</artifactId>              <version>3.2.1</version>          </dependency>            <dependency>              <groupId>commons-codec</groupId>              <artifactId>commons-codec</artifactId>              <version>1.10</version>          </dependency>          <!-- -->          <dependency>              <groupId>log4j</groupId>              <artifactId>log4j</artifactId>              <version>1.2.12</version>          </dependency>      </dependencies>      <dependencyManagement>          <dependencies>              <dependency>                  <groupId>javax.servlet</groupId>                  <artifactId>javax.servlet-api</artifactId>                  <version>3.0.1</version>                  <scope>provided</scope>              </dependency>              <dependency>                  <groupId>redis.clients</groupId>                  <artifactId>jedis</artifactId>                  <version>2.8.0</version>              </dependency>              <dependency>                  <groupId>org.apache.struts</groupId>                  <artifactId>struts2-core</artifactId>                  <version>2.2.3.1</version>              </dependency>              <dependency>                  <groupId>org.apache.tomcat</groupId>                  <artifactId>tomcat-catalina</artifactId>                  <version>7.0.57</version>                  <scope>provided</scope>              </dependency>              <dependency>                  <groupId>org.apache.tomcat</groupId>                  <artifactId>tomcat-coyote</artifactId>                  <version>7.0.57</version>                  <scope>provided</scope>              </dependency>          </dependencies>      </dependencyManagement>      <!--<scm>          <connection>scm:git:git@git.qwexyz.com:qwexyz/qwexyz.git</connection>          <url>scm:git:git@git.qwexyz.com:qwexyz/qwexyz.git</url>          <developerConnection>scm:git:git@git.qwexyz.com:qwexyz/qwexyz.git</developerConnection>      </scm>-->      <build>          <plugins>              <plugin>                  <artifactId>maven-compiler-plugin</artifactId>                  <version>3.1</version>                  <configuration>                      <source>1.6</source>                      <target>1.6</target>                      <encoding>UTF-8</encoding>                  </configuration>              </plugin>              <plugin>                  <artifactId>maven-resources-plugin</artifactId>                  <version>2.6</version>                  <configuration>                      <encoding>UTF-8</encoding>                  </configuration>              </plugin>              <plugin>                  <groupId>org.apache.maven.plugins</groupId>                  <artifactId>maven-release-plugin</artifactId>                  <version>2.1</version>              </plugin>          </plugins>      </build>    </project>  

      web

    Xml代码   <?xml version="1.0" encoding="UTF-8"?>  <project xmlns="http://maven.apache.org/POM/4.0.0"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">      <parent>          <artifactId>ixhong</artifactId>          <groupId>com.ixhong</groupId>          <version>1.0-SNAPSHOT</version>      </parent>      <modelVersion>4.0.0</modelVersion>        <artifactId>ixhong-lender-web</artifactId>        <packaging>war</packaging>      <name>ixhong-user-web</name>      <url>http://maven.apache.org</url>      <!-- 依赖包 -->      <dependencies>            <dependency>              <groupId>com.ixhong</groupId>              <artifactId>ixhong-manager</artifactId>              <version>1.0-SNAPSHOT</version>          </dependency>          <dependency>              <groupId>com.ixhong</groupId>              <artifactId>ixhong-common</artifactId>              <version>1.0-SNAPSHOT</version>          </dependency>          <dependency>              <groupId>org.mybatis</groupId>              <artifactId>mybatis-spring</artifactId>              <version>1.2.2</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-context</artifactId>              <version>${spring.version}</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-context-support</artifactId>              <version>${spring.version}</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-webmvc</artifactId>              <version>${spring.version}</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-jdbc</artifactId>              <version>${spring.version}</version>          </dependency>          <dependency>              <groupId>mysql</groupId>              <artifactId>mysql-connector-java</artifactId>              <version>5.1.32</version>          </dependency>            <dependency>              <groupId>org.apache.velocity</groupId>              <artifactId>velocity</artifactId>              <version>1.7</version>          </dependency>          <dependency>              <groupId>javax.servlet</groupId>              <artifactId>javax.servlet-api</artifactId>              <version>3.0.1</version>              <scope>provided</scope>          </dependency>              <dependency>              <groupId>commons-dbcp</groupId>              <artifactId>commons-dbcp</artifactId>              <version>1.4</version>          </dependency>          <dependency>              <groupId>commons-fileupload</groupId>              <artifactId>commons-fileupload</artifactId>              <version>1.3.1</version>          </dependency>          <dependency>              <groupId>org.apache.velocity</groupId>              <artifactId>velocity-tools</artifactId>              <version>2.0</version>              <exclusions>                  <exclusion>                      <groupId>org.apache.struts</groupId>                      <artifactId>*</artifactId>                  </exclusion>              </exclusions>          </dependency>            <dependency>              <groupId>org.apache.activemq</groupId>              <artifactId>activemq-client</artifactId>              <version>5.11.1</version>          </dependency>          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-jms</artifactId>              <version>${spring.version}</version>          </dependency>                <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-test</artifactId>              <version>${spring.version}</version>              <scope>test</scope>          </dependency>          <dependency>              <groupId>junit</groupId>              <artifactId>junit</artifactId>              <version>4.8.2</version>              <scope>test</scope>          </dependency>        </dependencies>      <build>          <finalName>ROOT</finalName>          <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>          <resources>              <resource>                  <directory>src/main/resources</directory>                  <filtering>true</filtering>              </resource>          </resources>      </build>        <profiles>          <profile>              <id>development</id>              <activation>                  <activeByDefault>true</activeByDefault>              </activation>              <build>                  <filters>                      <filter>../profile/development.properties</filter>                  </filters>              </build>          </profile>          <profile>              <id>test</id>              <build>                  <filters>                      <filter>../profile/test.properties</filter>                  </filters>              </build>          </profile>          <profile>              <id>production</id>              <build>                  <filters>                      <filter>../profile/production.properties</filter>                  </filters>              </build>          </profile>      </profiles>      </project>  

      dao

    Xml代码   <?xml version="1.0" encoding="UTF-8"?>  <project xmlns="http://maven.apache.org/POM/4.0.0"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">      <parent>          <artifactId>ixhong</artifactId>          <groupId>com.ixhong</groupId>          <version>1.0-SNAPSHOT</version>      </parent>      <modelVersion>4.0.0</modelVersion>        <artifactId>ixhong-dao</artifactId>        <packaging>jar</packaging>      <name>ixhong-dao</name>      <url>http://maven.apache.org</url>      <build>          <finalName>ixhong-dao</finalName>          <resources>              <resource>                  <directory>src/main/resources</directory>                  <filtering>true</filtering>              </resource>          </resources>      </build>      <dependencies>          <dependency>              <groupId>com.ixhong</groupId>              <artifactId>ixhong-domain</artifactId>              <version>1.0-SNAPSHOT</version>          </dependency>          <dependency>              <groupId>org.mybatis</groupId>              <artifactId>mybatis</artifactId>              <version>${ibatis.version}</version>          </dependency>        </dependencies>      </project>  

      domain

    Xml代码   <?xml version="1.0" encoding="UTF-8"?>  <project xmlns="http://maven.apache.org/POM/4.0.0"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">      <parent>          <artifactId>ixhong</artifactId>          <groupId>com.ixhong</groupId>          <version>1.0-SNAPSHOT</version>      </parent>      <modelVersion>4.0.0</modelVersion>        <artifactId>ixhong-domain</artifactId>        <packaging>jar</packaging>      <name>ixhong-domain</name>      <url>http://maven.apache.org</url>      <build>          <finalName>ixhong-domain</finalName>      </build>      <dependencies>          <dependency>              <groupId>net.sf.json-lib</groupId>              <artifactId>json-lib</artifactId>              <version>2.4</version>              <classifier>jdk15</classifier>          </dependency>      </dependencies>      </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代码   <?xml version="1.0" encoding="UTF-8"?>  <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"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    <profiles>      <profile>        <repositories>          <repository>            <snapshots>              <enabled>false</enabled>            </snapshots>            <id>central</id>            <name>libs-releases</name>            <url>http://maven.whatsmars.com/libs-releases</url>          </repository>          <repository>            <snapshots />            <id>snapshots</id>            <name>libs-snapshots</name>            <url>http://maven.whatsmars.com/libs-snapshots</url>          </repository>        </repositories>        <pluginRepositories>          <pluginRepository>            <snapshots>              <enabled>false</enabled>            </snapshots>            <id>central</id>            <name>plugins-releases</name>            <url>http://maven.whatsmars.com/plugins-releases</url>          </pluginRepository>          <pluginRepository>            <snapshots />            <id>snapshots</id>            <name>plugins-snapshots</name>            <url>http://maven.whatsmars.com/plugins-snapshots</url>          </pluginRepository>        </pluginRepositories>        <id>artifactory</id>      </profile>    </profiles>    <activeProfiles>      <activeProfile>artifactory</activeProfile>    </activeProfiles>  </settings>  

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

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

     

    maven可继承的pom元素

    Xml代码   groupId :项目组 ID ,项目坐标的核心元素;  version :项目版本,项目坐标的核心元素;  description :项目的描述信息;  organization :项目的组织信息;  inceptionYear :项目的创始年份;  url :项目的 url 地址  develoers :项目的开发者信息;  contributors :项目的贡献者信息;  distributionManagerment :项目的部署信息;  issueManagement :缺陷跟踪系统信息;  ciManagement :项目的持续继承信息;  scm :项目的版本控制信息;  mailingListserv :项目的邮件列表信息;  properties :自定义的 Maven 属性;  dependencies :项目的依赖配置;  dependencyManagement :醒目的依赖管理配置;  repositories :项目的仓库配置;  build :包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等;  reporting :包括项目的报告输出目录配置、报告插件配置等。   原文链接:[http://wely.iteye.com/blog/2270414 ]
    最新回复(0)