Eclipse 工作正常而java命令运行出错的一个坑

      昨天碰到一个让我很崩溃的问题。我在我的java程序里面调用MavenCli的API来执行一个maven命令。在Eclipse中运行一切正常。然后我用maven assebly把程序打成一个包含dependency的jar包,用java -jar 命令来执行这个程序的时候,却死活不工作了,出现以下的Exception:

 

 

org.codehaus.plexus.component.repository.exception.ComponentLookupException: com.google.inject.ProvisionException: Unable to provision, see the following errors:

 1) No implementation for org.eclipse.aether.RepositorySystem was bound.

   while locating org.apache.maven.artifact.resolver.DefaultArtifactResolver

   at ClassRealm[plexus.core, parent: null] (via modules: org.eclipse.sisu.wire.WireModule -> org.eclipse.sisu.plexus.PlexusBindingModule)

   at ClassRealm[plexus.core, parent: null] (via modules: org.eclipse.sisu.wire.WireModule -> org.eclipse.sisu.plexus.PlexusBindingModule)

   while locating org.apache.maven.artifact.resolver.ArtifactResolver

   while locating org.apache.maven.repository.legacy.LegacyRepositorySystem

   at ClassRealm[plexus.core, parent: null] (via modules: org.eclipse.sisu.wire.WireModule -> org.eclipse.sisu.plexus.PlexusBindingModule)

   at ClassRealm[plexus.core, parent: null] (via modules: org.eclipse.sisu.wire.WireModule -> org.eclipse.sisu.plexus.PlexusBindingModule)

   while locating org.apache.maven.repository.RepositorySystem

   ......  

 

       从这些错误看起来,像是少了什么Dependency。所以我就以为是Maven的Dependency有版本冲突,花了很多时间来替换测试,结果一点都没有用。

 

       后来在绝望之下,我只能尝试用maven exec:java的命令来运行这个程序。Bingo,工作了!后来跟同事交谈,了解到Assembly把Dependency的class文件全部打到jar里,有些情况下可能会导致class的冲突。下次知道了,如果还碰到这种情况,记得用maven exec:java命令来试试哦。

 

P.S.以下是我用来打包的maven assembly配置:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
     <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
     </descriptorRefs>
     <archive>
      <manifest>
       <mainClass>com.ebay.raptor.tools.DependencyValidator</mainClass>
      </manifest>
     </archive>
    </configuration>
    <executions>
     <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
       <goal>single</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

 

你可能感兴趣的:(java,maven)