maven一些問題


maven一些問題 - ljhzzyx的日志 - 網易博客

1. The container 'Maven Dependencies' references non existing library
解決方法,將eclipse中maven插件中“resolve dependencies from workspace projects”的選項取消
默認的本地庫更改,修改maven_home\conf\settings.xml中localRepository的配置
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (...). Please verify you invoked Maven from the correct directory. 
到有pom.xml文件的目錄下執行命令
2.      myeclipse從svn導入maven項目,有modules。
myeclipse的check out maven projects from scm,沒有試成功怎么導出。但可以先從svn檢查maven項目,包含子module,然后再導入exists maven projects。
3. Failure to find xxx:jar:1.0 in ... was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced
      這是一些jar包在之前的倉庫無法獲得,在更改倉庫后,或把jar包部署到了私服上后,仍然出錯。這時把.m2 文件夾下對應的 xxx.lastUpdated文件再更新依賴,就可以了。或者使用mvn命令時加 -U參數,忽略xxx.lastUpdated。

      另外以上方式仍然報錯,這時可能是maven 庫或私服里確實沒有對應的jar包,這時可以在私服里增加倉庫,或者更改倉庫。
4.      pom聚合的install,在maven 的build config里,goals選項里填入install,profiles中填入maven聚合項目中配置的modules profile的id,如果是如下配置:
<profiles>
 <profile>
    <id>modules</id>
    <modules>
<module>modules/parent</module>
<module>modules/common</module>
<module>modules/entity</module>
<module>modules/ejb</module>
<module>modules/spring</module>
<module>modules/hibernate</module>
<module>modules/web</module>
<module>modules/test</module>
  </modules>
 </profile>
</profiles>
則這里就要填入modules
5.  maven install的plugin找不到,比如effective pom 中配的是2.3.1,本機上已經有2.2.1了,是不會使用的。就會去maven倉庫中下載新版本,maven 的中央庫連接經常不穩定。一般會在settings.xml中配置一些鏡像。但maven不會依次去嘗試所有鏡像,而當排在前面的鏡像失效、或不能提供最新版本時,就需要調整鏡像的位置。
6.  導入的maven項目找不到jdk中的類
jdk版本是1.6,ide是ide出問題了,將Myeclipse重啟ide,對有依賴的maven項目進行更新maven依賴、更新maven項目配置操作,然后對本項目clean、 重新build后就可以了。
7.  拷貝依賴包 mvn dependency:copy-dependencies,默認會拷到項目的 target\dependency 目錄,想要復制到自定義的目錄比如target/libs目錄下,需要在pom.xml文件中添加設置覆蓋默認設置:
<build> 
    <plugins> 
        <plugin> 
            <artifactId>maven-dependency-plugin</artifactId> 
            <configuration> 
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <excludeTransitive>false</excludeTransitive> 
                <stripVersion>true</stripVersion> 
            </configuration> 
        </plugin> 
其中${project.build.directory}是maven變量,表示target目錄。如果不寫的話,將在根目錄下創建lib目錄。
excludeTransitive,表示是否不包含間接依賴的包;
stripVersion表示復制的jar文件去掉版本信息。
如果需要在其他過程,比如package中加入copy-dependencies,需要在該plugin標簽中這樣設置:
<executions> 
    <execution> 
        <id>copy-dependencies</id> 
        <phase>package</phase> 
        <goals> 
            <goal>copy-dependencies</goal> 
        </goals> 
        <configuration> 
            <outputDirectory>libs</outputDirectory> 
            <excludeTransitive>false</excludeTransitive> 
            <stripVersion>true</stripVersion> 
        </configuration> 
    </execution> 
</executions>
8. 導入jar包到本地庫
      有時候有些第三方就是從倉庫中下載不下來,配的私服也有問題,就只有手動導入jar包了。Myeclipse 10 可以使用“Myeclipse -- utilities -- Maven4MyEclipse -- import jar to local repository”工具,導入jar包到本地庫。eclipse的m2eclipse插件暫時沒找到這樣的工具。使用maven命令的可以這樣:
mvn install:install-file 
-DgroupId=com.danga 
-DartifactId=memcached 
-Dversion=2.0.1 
-Dfile=java_memcached-release_2.0.1.jar (或者/d:/java_memcached-release_2.0.1.jar)
-Dpackaging=jar -DgeneratePom=true
This will add the memcache jar into your local Maven2 repository under groupId com.danga and artifactId memcached, you can then edit your pom.xml adding this dependency.
However, the maven eclipse can not recognize it since it always search from public repository .
安裝到私服
    mvn deploy:deploy-file -DgroupId=org.apache.hadoop  -DartifactId=hbase -Dversion=1.0 -Dpackaging=jar -Dfile=[path to file] -Durl=[url] -DrepositoryId=[id]
批量導入jar
    直接拷貝文件至/opt/data/nexus/sonatype-work/nexus/storage/pvinsight/org/apache/hadoop/hive/hive-exec/0.5.0
    通過腳本執行 mvn deploy:deploy-file  
9. 使用maven發布時,報某個類找不到,原因是某個運行時的類,import了用於測試的類(但並未使用),比如junit相關的類。這樣,在ide中和maven compile時都不會報錯,在打包時,由於junit這樣的jar的scope是test類型,因此就不會包含進來,於是就出錯了。解決方法就是刪除不不要的引用。
10. 使用mvn compile命令,出現
錯誤: 編碼GBK的不可映射字符
不能編譯。這是因為代碼或注釋中存在中文引起的,一般在ide中會自動處理編譯時的字符集,就不會碰到這個錯誤。這個錯誤是在生成代碼后,其中自動加上了中文注釋,手動刪除中文注釋處理這個問題太麻煩。這個錯誤是在命令行執行編譯命令才出現的,需要設置編譯的字符集,設置方式是:
maven編譯文件的編碼設置如下:
<plugin> 
    <artifactId>maven-compiler-plugin</artifactId> 
    <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
        <encoding>UTF-8</encoding> 
    </configuration> 
</plugin> 
<encoding>UTF-8</encoding>,如果不設置的話會用本地操作系統的編碼來編譯文件。
資源文件的編碼設置如下:
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-resources-plugin</artifactId> 
    <version>2.3</version> 
    <configuration> 
        <encoding>UTF-8</encoding> 
    </configuration> 
</plugin>
設置好maven-compiler-plugin編碼再運行mvn compile就沒有這個錯誤了。
11. maven打包時,SNAPSHOT類型的依賴包,會加上時間戳,像下面這樣
xxx-1.3.0-20121225.012733.jar
如果不想用時間戳,始終使用SNAPSHOT,xxx-1.3.0-SNAPSHOT.jar這樣的,需要使用<useUniqueVersions>false</useUniqueVersions>配置。在assemble插件里有描述: http://maven.apache.org/shared/maven-archiver/examples/classpath.html。而有完整例子的在這個日文網站 http://kenichiro22.hatenablog.com/entry/20110908/1315481232
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.xxx.Main</mainClass>
<useUniqueVersions>false</useUniqueVersions>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
這樣打包后jar文件中的MANIFEST.MF文件中的依賴描述就是用SNAPSHOT而不是用時間戳了。
我只用到了第一個問題。本文轉自
http://ljhzzyx.blog.163.com/blog/static/38380312201110304566706/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM