Maven - 为啥在“mvn clean”之后我需要在“mvn package”之前执行“Maven Update Project”?

Posted

技术标签:

【中文标题】Maven - 为啥在“mvn clean”之后我需要在“mvn package”之前执行“Maven Update Project”?【英文标题】:Maven - Why after "mvn clean" I need to execute "Maven Update Project" before "mvn package"?Maven - 为什么在“mvn clean”之后我需要在“mvn package”之前执行“Maven Update Project”? 【发布时间】:2013-03-20 19:06:35 【问题描述】:

我正在做一些测试,我想了解为什么在执行命令mvn clean 后我需要在运行mvn package 之前执行“Maven > 更新项目”,否则我的一个依赖项中会出现编译错误在包装过程中。

看起来我的项目在运行“Maven > 更新项目”之前没有正确编译我的项目,但我不确定。 这正常吗?

我正在使用 Eclipse 并通过“Run as > Maven Build”运行命令 mvn clean 和 mvn package。

这是我的 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>com.project</groupId>
    <artifactId>myProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <jmockit.version>0.0.1</jmockit.version>
    </properties>

    <build>
        <testSourceDirectory>src/test/java/junit</testSourceDirectory>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>

        <testResources>
            <testResource>
                <directory>src/test/java/selenium</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
        </testResources>

        <pluginManagement>
            <plugins>
                <!-- Need Java 5, which is the default since v2.3 of the maven-compiler-plugin. -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5.1</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <warSourceExcludes>src/main/resources/conf_development</warSourceExcludes>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/conf_production</directory>
                            <targetPath>WEB-INF/classes/</targetPath>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </resource>
                        <resource>
                            <directory>src/main/resources/conf</directory>
                            <targetPath>WEB-INF/classes/</targetPath>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <argLine>
                        -javaagent:"$settings.localRepository"/mockit/jmockit/$jmockit.version/jmockit-$jmockit.version.jar
                    </argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>


<dependencies>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>1.5.3</version>
    </dependency>
    <dependency>
        <groupId>bouncycastle</groupId>
        <artifactId>bcprov-jdk14</artifactId>
        <version>140</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0-oracle-thin-extras</artifactId>
        <version>0.9.0.2</version>
    </dependency>
    <dependency>
        <groupId>commons-betwixt</groupId>
        <artifactId>commons-betwixt</artifactId>
        <version>0.7</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-email</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.ehcache-spring-annotations</groupId>
        <artifactId>ehcache-spring-annotations</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.4.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.4.0.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.2.GA</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.1.2</version>
    </dependency>
    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>2.5.6</version>
    </dependency>
    <dependency>
        <groupId>net.fckeditor</groupId>
        <artifactId>java-core</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>backport-util-concurrent</groupId>
        <artifactId>backport-util-concurrent</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.ibm.icu</groupId>
        <artifactId>icu4j</artifactId>
        <version>3.8</version>
    </dependency>
    <dependency>
        <groupId>mockit</groupId>
        <artifactId>jmockit</artifactId>
        <version>$jmockit.version</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>com.lowagie</groupId>
        <artifactId>itext</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>atg.taglib.json</groupId>
        <artifactId>json-taglib</artifactId>
        <version>0.4.1</version>
    </dependency>
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.jexcelapi</groupId>
        <artifactId>jxl</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.8.5</version>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>1.8.4</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>2.25.0</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6.SEC03</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-agent</artifactId>
        <version>2.5.6.SEC03</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>2.5.6.SEC03</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-config-browser-plugin</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-convention-plugin</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-jasperreports-plugin</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-jfreechart-plugin</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-json-plugin</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-spring-plugin</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.6.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-tools</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>com.yahoo.platform.yui</groupId>
        <artifactId>yuicompressor</artifactId>
        <version>2.4.6</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>net.sf.jodreports</groupId>
        <artifactId>jodreports</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-oxm</artifactId>
        <version>1.5.9</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-ws-core</artifactId>
        <version>1.5.9</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-oxm-tiger</artifactId>
        <version>1.5.9</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-ws-core-tiger</artifactId>
        <version>1.5.9</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-xjc</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.ws.commons.axiom
        </groupId>
        <artifactId>axiom-api</artifactId>
        <version>1.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.ws.commons.axiom
        </groupId>
        <artifactId>axiom-impl</artifactId>
        <version>1.2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.xml</groupId>
        <artifactId>jaxrpc-api</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium.client-drivers</groupId>
        <artifactId>selenium-java-client-driver</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>org.acegisecurity</groupId>
        <artifactId>acegi-security</artifactId>
        <version>1.0.7</version>
    </dependency>
    <dependency>
        <groupId>citizen</groupId>
        <artifactId>citizen</artifactId>
        <version>citizen</version>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.4</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jasperreports</groupId>
        <artifactId>jasperreports</artifactId>
        <version>3.5.3</version>
    </dependency>
</dependencies>
</project>

错误:

(记住有错误的库是一个内部库,在“Maven > 更新项目”之后它工作得很好)

[INFO] Compiling 2595 source files to C:\Users\user\Documents\Dev\workspace\project\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] C:\Users\user\Documents\Dev\workspace\project\src\main\java\com\project\jobs\citizen\model\service\CitizenService.java:[38
,34] cannot access org.apache.axis.client.Service
class file for org.apache.axis.client.Service not found
        Resolution2Locator r2 = new Resolution2Locator ();
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2:09.448s
[INFO] Finished at: Fri Mar 22 11:55:14 BRT 2013
[INFO] Final Memory: 25M/274M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project project: Compilation failure
[ERROR] C:\Users\user\Documents\Dev\workspace\project
\src\main\java\com\project\jobs\citizen\model\service\CitizenService.java:[38
,34] cannot access org.apache.axis.client.Service
[ERROR] class file for org.apache.axis.client.Service not found
[ERROR] Resolution2Locator r2 = new Resolution2Locator();
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project u
nico: Compilation failure
C:\Users\user\Documents\Dev\workspace\project\src\main\java\com\project\jobs\citizen\model\service\CitizenService.java:[38,34] can
not access org.apache.axis.client.Service
class file for org.apache.axis.client.Service not found
        Resolution2Locator r2 = new Resolution2Locator ();

    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
C:\Users\user\Documents\Dev\workspace\project\src\main\java\com\project\jobs\citizen\model\service\CitizenService.java:[38,34] can
not access org.apache.axis.client.Service
class file for org.apache.axis.client.Service not found
        Resolution2Locator r2 = new Resolution2Locator ();

    at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:729)
    at org.apache.maven.plugin.CompilerMojo.execute(CompilerMojo.java:128)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    ... 19 more
[ERROR] 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

【问题讨论】:

只是为了确认:您使用的是 eclipse 吗?你是否从 eclipse 运行 mvn cleanmvn package ?你得到什么样的错误? 是的,我正在使用 Eclipse 并通过“Run as > Maven Build”运行命令 mvn clean 和 mvn package。我的一个依赖项出现编译错误。 maven-eclipse 集成完全是错误的。如果您正在学习 maven,我强烈建议您使用命令行或任何其他 IDE。 (即,您需要运行更新依赖项的事实是因为 eclipse 中的那些错误,仅在运行 maven 时不需要) 真的吗?好吧,我会在命令行中尝试并告诉你结果。 我认为maven配置或项目的pom.xml有问题。在添加了所需类的依赖项后,我成功地用 maven 编译了项目。但它是传递依赖。甚至依赖插件都报告说没有必要。 【参考方案1】:

会发生什么:

    mvn clean 删除target 目录 mvn package 执行了一些工作,但由于缺少依赖项而遇到编译错误。这就是目标文件夹出错的原因。 运行 Maven > 在 Eclipse 中更新项目会导致 Eclipse 执行自己的编译,与 Maven 分开。 Eclipse 的类路径与 Maven 不同,因此它的编译成功。现在target 目录中包含了所有已编译的类。 现在重新运行mvn package 会成功,因为它会找到所有已编译的类,不需要执行任何编译。

归根结底,问题出在pom.xml中的项目配置上。我遇到了同样的问题,在添加缺少的依赖项后,我成功地用 Maven 编译了项目。

【讨论】:

刚刚添加了这个依赖,一切正常!非常感谢! &lt;dependency&gt; &lt;groupId&gt;org.apache.axis&lt;/groupId&gt; &lt;artifactId&gt;axis&lt;/artifactId&gt; &lt;version&gt;1.4&lt;/version&gt; &lt;/dependency&gt; 很好解释,很有帮助!谢谢【参考方案2】:

Eclipse m2e 插件(这是一个简化版)读取 POM 文件并将其转换为 Eclipse 的指令,以便 Eclipse 可以完成它的工作。因为 Eclipse 始终处于控制之中,所以您的视图会正确更新并且事情非常顺利。 m2e 不支持带有插件的一些更高级的 Maven 操作,因此这些操作不会发生。一些简单的东西,比如类路径与 Maven 做事的方式不同——m2e 无论如何都使用“测试”类路径,因为 Eclipse 无法预测你想要运行什么。

另一方面,执行“Run as > Maven ...”实际上调用了完全在 Eclipse 之外的程序 Maven,并要求它运行。 Eclipse 不会自动知道 Maven 何时创建或删除了文件。

我通常会远离“Run as > Maven”命令。集成很差,Eclipse 控制台界面很丑,如果你需要运行特定的 Maven 目标,从命令行调用mvn 会更容易。

如果你只是想开发一个 Maven 项目,我发现 m2e 集成效果很好。我使用 Jenkins 为我处理打包和部署,所以我只使用 Eclipse 来编辑和测试我的代码。

【讨论】:

非常感谢您的解释!!它的想法是与 Jenkins 一起运行它,但在迁移未完成之前,我们将需要在本地执行这些命令。不幸的是,当我直接从提示符执行mvn clean 时,也会发生同样的错误。你知道我应该怎么做吗?再次感谢。 @PimentaDev。运行mvn clean 时看到的错误是什么? mvn clean 运行良好。问题出在mvn package 之后。我的一个库出现编译错误,但我不知道为什么。只有当我打开 Eclipse 并运行“Maven > 更新项目”时,错误才会停止! 这些听起来像是远离 Eclipse 的重要理由。 IntelliJ 让 Maven 运行构建没有问题。 @GregChabala 对于需要使用 Maven 做一些复杂或奇怪的事情的项目,我不会推荐 Eclipse。但是对于已经熟悉 Eclipse 并且只需要能够清理和构建项目的用户来说,m2e 非常棒。【参考方案3】:

如果有任何新的更新可用,它将更新文件。你可以在 maven clean 之后使用 maven install

【讨论】:

但是为什么命令mvn package 有效?并且不需要进行任何更新,只需重建项目即可。

以上是关于Maven - 为啥在“mvn clean”之后我需要在“mvn package”之前执行“Maven Update Project”?的主要内容,如果未能解决你的问题,请参考以下文章

maven在执行mvn clean install -Dmaven.test.skip=true命令的时候报的这个错这个怎么解决呢在线高分等

为啥我在运行 mvn clean install 时出现 errorRebind 结果“javax.validation.ValidatorFactory”必须是一个类?

xml 使用依赖项构建Maven - mvn clean包程序集:单个

当尝试 mvn clean install -> 出现错误包不存在

豁然明白的囧事 之 执行mvn:clean deploy提示Unauthorized

豁然明白的囧事 之 执行mvn:clean deploy提示Unauthorized