转载

Maven问题记录

Maven问题解决记录:

1、远程仓库中有jar,但是编译时找不到。

问题描述:

Failed to execute goal on project amps-dcm: Could not resolve dependencies for project com.zollty.pss:amps-dcm:war:0.0.1-SNAPSHOT: The following artifacts could not be resolved: org.zollty.util:zollty-util:jar:1.0, org.zollty.log:zollty-log:jar:1.0: Failure to find org.zollty.util:zollty-util:jar:1.0 in http://172.27.18.106:8081/nexus/content/groups/public was cached in the local repository, resolution will not be reattempted until the update interval of LocalMirrorId has elapsed or updates are forced -> [Help 1]

[ERROR]

[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.

[ERROR]

解决方案:

打开本地仓库目录,找到那个jar的位置,把残留的文件夹全都删除。然后再重新下载这个jar。

还是不行?有可能是parent项目中也存在xxx.lastUpdate的文件。所以把parent项目也清理一下。

例如:

<parent>

<groupId>org.zollty</groupId>

<artifactId>zollty-org</artifactId>

<version>1.0</version>

</parent>

那么,请检查

D:/C/Java/maven3/repo/org/zollty/zollty-org/1.0目录!!

2、transitive dependencies (if any) will not be available, enable debug logging for more details

详细错误如下:

[WARNING] The POM for org.zollty.util:zollty-util:jar:1.0 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details

解决方法: 显式引入:zollty-util:jar:1.0依赖的jar即可。

3、如何将Maven依赖的jar发布到tomcat的WEB-INF/lib下面

在.classpath文件中设置:

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">

<attributes>

<attribute name="maven.pomderived" value="true"/>

<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>

</attributes>

</classpathentry>

注意,有些插件是:<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER">

4、配置了拷贝(overlays)引入的war包的Eclipse-Maven 项目,当发布到服务器上时,没有拷贝到war中的文件到发布的服务器上。

pom配置如下:

<plugin>

    <groupId>org.apache.maven.plugins</groupId>

    <artifactId>maven-war-plugin</artifactId>

    <version>2.1.1</version>

    <configuration>

     <attachClasses>true</attachClasses>

     <overlays>

      <overlay>

       <groupId>com.zollty.pss</groupId>

       <artifactId>abframe</artifactId>

      </overlay>

      <overlay>

       <!-- don't remove this tag. empty groupId/artifactId represents the current build -->

      </overlay>

     </overlays>

    </configuration>

   </plugin>

原因: 没有配置支持overlays的Eclipse插件,即m2e-wtp插件。

有些Eclipse插件是这样

.project文件

<natures>

...

<nature>org.maven.ide.eclipse.maven2Nature</nature>

...

</natrues>

.classpath 文件

<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER">

<attributes>

<attribute name="maven.pomderived" value="true"/>

<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>

</attributes>

</classpathentry>

但是有些Eclipse插件是这样

.project文件

<natures>

...

<nature>org.eclipse.m2e.core.maven2Nature</nature>

...

</natures>

.classpath 文件

<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">

<attributes>

<attribute name="maven.pomderived" value="true"/>

<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>

</attributes>

</classpathentry>

经测试,第二个配置可以正确使用maven的war包依赖自动发布的功能。

如果是第一个配置,那需要安装m2e-wtp插件。

安装后,完整的配置如下

.project文件

<? xml version = "1.0" encoding = "UTF-8" ?>

< projectDescription >

  < name >amps-dcm</ name >

  < comment ></ comment >

  < projects >

  </ projects >

  < buildSpec >

  < buildCommand >

   < name >org.eclipse.wst.jsdt.core.javascriptValidator</ name >

   < arguments >

   </ arguments >

  </ buildCommand >

  < buildCommand >

   < name >org.eclipse.wst.common.project.facet.core.builder</ name >

   < arguments >

   </ arguments >

  </ buildCommand >

  < buildCommand >

   < name >com.genuitec.eclipse.j2eedt.core.WebClasspathBuilder</ name >

   < arguments >

   </ arguments >

  </ buildCommand >

  < buildCommand >

   < name >org.eclipse.jdt.core.javabuilder</ name >

   < arguments >

   </ arguments >

  </ buildCommand >

  < buildCommand >

   < name >com.genuitec.eclipse.j2eedt.core.J2EEProjectValidator</ name >

   < arguments >

   </ arguments >

  </ buildCommand >

  < buildCommand >

   < name >com.genuitec.eclipse.j2eedt.core.DeploymentDescriptorValidator</ name >

   < arguments >

   </ arguments >

  </ buildCommand >

  < buildCommand >

   < name >org.eclipse.wst.validation.validationbuilder</ name >

   < arguments >

   </ arguments >

  </ buildCommand >

  < buildCommand >

   < name >com.genuitec.eclipse.ast.deploy.core.DeploymentBuilder</ name >

   < arguments >

   </ arguments >

  </ buildCommand >

  < buildCommand >

   < name >org.eclipse.m2e.core.maven2Builder</ name >

   < arguments >

   </ arguments >

  </ buildCommand >

  </ buildSpec >

  < natures >

  < nature >org.eclipse.m2e.core.maven2Nature</ nature >

  < nature >org.eclipse.jem.workbench.JavaEMFNature</ nature >

  < nature >org.eclipse.wst.common.modulecore.ModuleCoreNature</ nature >

  < nature >com.genuitec.eclipse.ast.deploy.core.deploymentnature</ nature >

  < nature >com.genuitec.eclipse.j2eedt.core.webnature</ nature >

  < nature >org.eclipse.jdt.core.javanature</ nature >

  < nature >org.eclipse.wst.common.project.facet.core.nature</ nature >

  < nature >org.eclipse.wst.jsdt.core.jsNature</ nature >

  </ natures >

</ projectDescription >

.classpath 文件

<? xml version = "1.0" encoding = "UTF-8" ?>

< classpath >

  < classpathentry kind = "src" output = "target/classes" path = "src/main/java" >

  < attributes >

   < attribute name = "optional" value = "true" />

   < attribute name = "maven.pomderived" value = "true" />

  </ attributes >

  </ classpathentry >

  < classpathentry excluding = "**" kind = "src" output = "target/classes" path = "src/main/resources" >

  < attributes >

   < attribute name = "maven.pomderived" value = "true" />

  </ attributes >

  </ classpathentry >

  < classpathentry kind = "src" output = "target/test-classes" path = "src/test/java" >

  < attributes >

   < attribute name = "optional" value = "true" />

   < attribute name = "maven.pomderived" value = "true" />

  </ attributes >

  </ classpathentry >

  < classpathentry excluding = "**" kind = "src" output = "target/test-classes" path = "src/test/resources" >

  < attributes >

   < attribute name = "maven.pomderived" value = "true" />

  </ attributes >

  </ classpathentry >

  < classpathentry

  kind = "con"

path = "org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6" >

  < attributes >

   < attribute name = "maven.pomderived" value = "true" />

  </ attributes >

  </ classpathentry >

  < classpathentry kind = "con" path = "org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" >

  < attributes >

   < attribute name = "maven.pomderived" value = "true" />

   < attribute name = "org.eclipse.jst.component.dependency" value = "/WEB-INF/lib" />

  </ attributes >

  </ classpathentry >

  < classpathentry kind = "output" path = "target/classes" />

</ classpath >

5、maven 无法确定 <T>T 的类型参数;对于上限为 long,java.lang.Object 的类型变量 T

无法确定 <X>X 的类型参数;对于上限为 X,java.lang.Object 的类型变量 X,不存在唯一最大实例

原因是,默认编译器(javac)的问题。解决方案:使用其他编译器,比如eclipse所用的编译器(JDT compiler from eclipse)。

在pom.xml中配置如下:

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.1</version>

<configuration>

<source>${java-version}</source>

<target>${java-version}</target>

<compilerId>eclipse</compilerId>

</configuration>

<dependencies>

<dependency>

<groupId>org.codehaus.plexus</groupId>

<artifactId>plexus-compiler-eclipse</artifactId>

<version>2.3</version>

</dependency>

</dependencies>

</plugin>

参见:

http://www.eclipse.org/forums/index.php/t/1229/

6、使用JDT compiler时出现 枚举类型switch报错 问题:

Cannot switch on a value of type. Only convertible int values or enum variables are permitted

例如:

class Foo {

   static enum MyEnum { A }

   void foo() {  switch ( MyEnum.A ) {  case A: } }

}

分析和解决方法:

Turns out that the setting org.eclipse.jdt.core.compiler.compliance needs to be set to the target version in order for it to be able to recognize  java.lang.Enum .

This setting is only set by plexus-compiler-eclipse when both targetVersion AND  optimize is set. [1]

Modify your pom like this and it should work:

< plugins >

  < plugin >

   < groupId >org.apache.maven.plugins</ groupId >

   < artifactId >maven-compiler-plugin</ artifactId >

   < version >3.1</ version <!-- or 3.0 -->

   < configuration >

     < compilerId >eclipse</ compilerId >

     < source >1.6</ source >

     < target >1.6</ target > < optimize >true</ optimize >    <!-- add this line! -->

I'm not sure why it was decided in plexus-compiler-eclipse that optimization would affect the compliance level, so this is in fact a workaround.

see:

https://github.com/sonatype/plexus-compiler/blob/master/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EclipseJavaCompiler.java#L156

7、Maven报错:'version' contains an expression but should be a constant.

是因为 父pom还没有发布到仓库中,建议在仓库中检查一下父pom是否正确。

8、Maven WTP无法引用工程中的项目模块(org.eclipse.wst.common.component)

也就是说,在org.eclipse.wst.common.component文件中缺少如下的语句:

<dependent-module archiveName="tre-client-2.0.2-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/tre-client/tre-client">

<dependency-type>uses</dependency-type>

</dependent-module>

那是因为 被引用的项目的pom配置有问题,经我检查,问题出在如下一行

<resources>

<resource>

<directory>${basedir}/src/main/resources</directory>

</resource>

<resource>

<directory>${basedir}/src/main/java/com/zollty/report/client</directory>

<includes>

<include>*.xsd</include>

</includes>

<targetPath>${project.build.directory}/classes/com/zollty/report/client</targetPath>

</resource>

</resources>

但是,在这个目录下面,找不到任何 .xsd 文件,运行maven指令的时候不会报错,但是在这个WTP中就检测到了这个问题,所以没有生成这个引用。

9、报错:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

原因:JRE环境指定的是JRE目录,而不是JDK目录。

解决方法:

● Click on Windows -> Preferences -> Java -> Installed JREs -> Add -> Standard VM and Select JAVA_HOME.

● Select New JRE from Installed JREs and Press OK

原文  https://blog.csdn.net/zollty/article/details/86135480
正文到此结束
Loading...