mvn package 将其他库加载为 Eclipse

Posted

技术标签:

【中文标题】mvn package 将其他库加载为 Eclipse【英文标题】:mvn package load other Library as Eclipse 【发布时间】:2017-04-12 04:37:53 【问题描述】:

如果我在 Eclipse 中运行我的项目,一切正常。但是当我这样做时:

mvn clean 
mvn package

然后启动我的项目然后它不工作。我发现了这个区别:

日食:

INFO  org.eclipse.jetty.server.Server:doStart:327 ~ jetty-9.2.13.v20150730

mvn 包:

INFO  org.eclipse.jetty.server.Server:doStart:327 ~ jetty-9.2.z-SNAPSHOT

为什么?我该怎么办,mvn package 加载相同的码头版本 9.2.13?

更新:

我发现了一些额外的差异:

在 Eclipse 中编译(WebApplicationInitializer):

org.eclipse.jetty.server.Server:doStart:327 ~ jetty-9.2.13.v20150730
org.eclipse.jetty.server.handler.ContextHandler$Context:log:2052 ~ No Spring WebApplicationInitializer types detected on classpath

mvn package(ServletContainerInitializers)编译:

org.eclipse.jetty.server.Server:doStart:327 ~ jetty-9.2.z-SNAPSHOT
org.eclipse.jetty.annotations.AnnotationConfiguration:configure:456 ~ ServletContainerInitializers: detected. Class hierarchy: empty

为什么 mvn 不使用 WebApplication?

POM.xml:

<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>de.myPro.test</groupId>
    <artifactId>AllInOneBoiler</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>AllInOneBoiler</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <log4j.version>2.7</log4j.version>
        <spring.version>4.3.3.RELEASE</spring.version>
        <camel.version>2.18.3</camel.version>
        <activemq.version>5.14.5</activemq.version>
        <jolokia.version>1.3.5</jolokia.version>
        <jetty.version>9.2.13.v20150730</jetty.version>
    </properties>

    <dependencies>
        <!-- Log4J Start -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>$log4j.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>$log4j.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>$log4j.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>$log4j.version</version>
        </dependency>
        <!-- Log4J Ende -->

        <!-- Camel Start -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>$camel.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
            <version>$camel.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>$camel.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-http4</artifactId>
            <version>$camel.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-mail</artifactId>
            <version>$camel.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-gson</artifactId>
            <version>$camel.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-saxon</artifactId>
            <version>$camel.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jdbc</artifactId>
            <version>$camel.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-sql</artifactId>
            <version>$camel.version</version>
        </dependency>
        <!-- Camel Ende -->

        <!-- Active MQ Start -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-client</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-openwire-legacy</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-camel</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-jaas</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-broker</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-console</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-shiro</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-spring</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-jms-pool</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-amqp</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-http</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-mqtt</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-stomp</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-kahadb-store</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-leveldb-store</artifactId>
            <version>$activemq.version</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-jdbc-store</artifactId>
            <version>$activemq.version</version>
        </dependency>

        <dependency>
            <groupId>org.apache.activemq.protobuf</groupId>
            <artifactId>activemq-protobuf</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.fusesource.hawtbuf</groupId>
            <artifactId>hawtbuf</artifactId>
            <version>1.11</version>
        </dependency>
        <dependency>
            <groupId>org.jasypt</groupId>
            <artifactId>jasypt</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-jms_1.1_spec</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-jta_1.0.1B_spec</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-j2ee-management_1.1_spec</artifactId>
            <version>1.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-annotation_1.0_spec</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-web</artifactId>
            <version>$activemq.version</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.activemq</groupId>
                    <artifactId>activemq-all</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Active MQ Ende -->

        <!-- Spring Start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</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-expression</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>$spring.version</version>
        </dependency>

        <!-- Spring Ende -->

        <!-- Jolokia Start -->
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
            <version>$jolokia.version</version>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-jvm</artifactId>
            <version>$jolokia.version</version>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-spring</artifactId>
            <version>$jolokia.version</version>
        </dependency>
        <!-- Jolokia Ende -->

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>$jetty.version</version>
        </dependency>
        <!-- Jetty Start -->
        <!-- <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-security</artifactId>
            <version>$jetty.version</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>$jetty.version</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-http</artifactId>
            <version>$jetty.version</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-annotations</artifactId>
            <version>$jetty.version</version>
        </dependency> -->


        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>apache-jstl</artifactId>
            <version>$jetty.version</version>
        </dependency> 
        <dependency>
            <groupId>org.eclipse.jetty.websocket</groupId>
            <artifactId>javax-websocket-server-impl</artifactId>
            <version>$jetty.version</version>
        </dependency> 
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>apache-jsp</artifactId>
            <version>$jetty.version</version>
        </dependency>
        <dependency>
            <groupId>org.ow2.asm</groupId>
            <artifactId>asm</artifactId>
            <version>5.1</version>
        </dependency>

        <!-- Jetty Ende -->
    </dependencies>

    <build>
        <finalName>BoilerPlate Standalone ActiveMQ and Camel</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-maven-plugin</artifactId>
                <version>$camel.version</version>
            </plugin>

            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>$jetty.version</version>
                <!-- <configuration>
                    <webAppConfig>
                        <contextPath> /$project.artifactId</contextPath >
                        <webInfIncludeJarPattern> busines-letter-*.</webInfIncludeJarPattern >
                    </webAppConfig>
                </configuration>  -->
                <dependencies>
                    <dependency>
                        <groupId>org.ow2.asm</groupId>
                        <artifactId>asm</artifactId>
                        <version>5.1</version>
                    </dependency>
                    <dependency>
                        <groupId>org.ow2.asm</groupId>
                        <artifactId>asm-commons</artifactId>
                        <version>5.1</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>

                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>

                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/cxf/bus-extensions.txt</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>$mainClass</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>org.apache.camel.spring.Main</mainClass>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                        <manifestSections>
                            <manifestSection>
                                <name>$project.name</name>
                                <manifestEntries>
                                    <version>$project.version</version>
                                </manifestEntries>
                            </manifestSection>
                        </manifestSections>
                    </archive>
                </configuration>
            </plugin>

            <plugin> <!-- Das Verzeichnis conf muss in diesem Projekt mit Kopiert werden, aber nicht in der JAR-Datei sein -->
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>move-main-class</id>
                        <phase>compile</phase>
                        <configuration>
                            <tasks>
                                <copy file="startEmbedded.bat" tofile="$project.build.directory/startEmbedded.bat"/>
                                <copy todir="$project.build.directory/conf">
                                    <fileset dir="conf/"/>
                                </copy>
                                <copy todir="$project.build.directory/webapps">
                                    <fileset dir="webapps/"/>
                                </copy>
                                <copy todir="$project.build.directory/lib">
                                    <fileset dir="lib/"/>
                                </copy>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

您介意分享您的 pom.xml 吗?你如何从 Eclipse 运行你的项目?如果您在 Eclipse 中有一个启动器,分享将帮助我们了解更多(您可以导出 laucher 并添加到您的问题中) 我插入了 pom.xml。我不认为我有一个发射器。我使用“运行配置”,但只有目标“骆驼:运行” 在eclipse编辑器中打开pom.xml,进入依赖选项卡,找到“jetty”,也许你间接有两次这个依赖。 是的,不止一个,但都使用相同的 9.2.13 版本 ???都有相同的版本! 【参考方案1】:

我怀疑您的.classpath 引用了硬编码的依赖版本,而不是从pom.xml 动态获取它们。这是在 Eclipse 中修复 maven 项目配置的可靠方法。

    右键项目=>配置=>转换为Maven项目(可能不需要这一步) 删除&lt;projectRoot&gt;/.classpath 如果你做了 2. 在 Eclipse 之外(例如 Windows 资源管理器),右键单击项目 =>“刷新” 右键项目=>Maven=>更新项目 应该创建一个没有硬编码依赖的新.classpath

【讨论】:

这并没有解决问题,抱歉。不过谢谢你的回答【参考方案2】:

尝试规范,

mvn clean
mvn generate-sources
mvn install

我过去曾遇到过关于“包”目标的问题。

【讨论】:

这没有帮助。同样的问题,在 Eclipse 下运行,但不是 .jar 文件【参考方案3】:

我怀疑有两个版本的 Maven 相互冲突。

    嵌入在 Eclipse IDE 中 外部配置使用。

确保两者相同并尝试再次运行打包,您将在两个地方得到相同的行为。

还要确保在 Preference->Maven 中正确引用了用户设置

【讨论】:

不,我使用相同的 Maven (3.2.3) 安装。不是嵌入式的 另外,您可以检查 Pom 文件查看器中的 Dependency Hierarchy 选项卡,这将指导您将jetty 版本传递到其他版本。与旧版本相比,maven 构建先于最新版本的可用性。【参考方案4】:

Eclipse 运行项目的方式与 maven 不同,它将所有项目类编译到一个文件夹(即 build 或 target)中,并将其作为 java 进程执行,其中包含该 build 文件夹的类路径和依赖项中的 jar。

您的 maven 配置会解压缩所有依赖项并将它们中的类放入您的 result.jar。类 mixin 会产生不可预测的结果,这就是码头打印错误版本的原因,因为包层次结构被破坏了。

package org.eclipse.jetty.util;

public class Jetty 
  public static final String VERSION;

  private Jetty() 
  

  static 
    Package pkg = Jetty.class.getPackage();
    if(pkg != null && "Eclipse.org - Jetty".equals(pkg.getImplementationVendor()) && pkg.getImplementationVersion() != null) 
      VERSION = pkg.getImplementationVersion();
     else 
      VERSION = System.getProperty("jetty.version", "9.2.z-SNAPSHOT");
    

  

【讨论】:

好的,我该怎么办? 您可以创建不包含所有依赖类的可执行jar。而是将所有依赖项 jar 放到名为“lib”的文件夹中,并在 Manifest.MF 的类路径中指定该库的路径。见maven.apache.org/shared/maven-archiver/examples/classpath.html 刚刚意识到你已经有了这个配置。您需要从 pom.xml 中删除 shade 插件,它不会将类复制到您的 jar 中。之后,您需要将所需的依赖项放到类路径指向的文件夹中。你可以像这里描述的那样做***.com/a/996915/936752 我认为你的答案是正确的,但我没有让它运行。现在我收到错误消息:.jar 中没有 mainmanifestattribute 好的,我用命令行“mvn camel:run”运行项目,但是 mvn 包不起作用。

以上是关于mvn package 将其他库加载为 Eclipse的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot:repackage 和 mvn package 的区别

mvn package 打包时加入本地jar包

mvn 打包命令

重定向“mvn package”或“mvn compile”命令的输出目录

“mvn clean install”和“mvn clean verify install”有啥区别?

加载与安装库的语法。