【问题标题】:Maven project builds fine in IntelliJ, but not with maven command lineMaven 项目在 IntelliJ 中构建良好,但不能使用 Maven 命令行
【发布时间】:2011-11-20 20:17:22
【问题描述】:

当我跑步时:

mvn

我收到以下错误:

'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 171, column 21
'build.plugins.plugin.version' for org.apache.maven.plugins:maven-war-plugin is missing. @ line 179, column 21

在构建失败块中我看到:

No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]

这是一个 spring mvc web 应用程序,我也刚刚添加了一个 Junit 测试(测试也使用 intellij 的测试运行器通过)

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>testapp1</groupId>
    <artifactId>testapp1</artifactId>
    <name>testapp1</name>
    <packaging>war</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <java-version>1.6</java-version>
        <org.springframework-version>3.0.6.RELEASE</org.springframework-version>
        <org.slf4j-version>1.6.1</org.slf4j-version>
    </properties>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                 </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>runtime</scope>
        </dependency>

         <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.5.1-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.1-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.5.1-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>3.3.1.GA</version>
        </dependency>

         <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.13</version>
        </dependency>



        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <repositories>
        <repository>
            <id>org.springframework.maven.release</id>
            <name>Spring Maven Release Repository</name>
            <url>http://maven.springframework.org/release</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
        <!-- For testing against latest Spring snapshots -->
        <repository>
            <id>org.springframework.maven.snapshot</id>
            <name>Spring Maven Snapshot Repository</name>
            <url>http://maven.springframework.org/snapshot</url>
            <releases><enabled>false</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <!-- For developing against latest Spring milestones -->
        <repository>
            <id>org.springframework.maven.milestone</id>
            <name>Spring Maven Milestone Repository</name>
            <url>http://maven.springframework.org/milestone</url>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
    </repositories>

     <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>testapp1</warName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.1</version>
            </plugin>
        </plugins>
    </build>
</project>

另外,我的单元测试失败了,因为 IntelliJ 没有解析语句:

int a = 1;
int b = 1;
assertTrue(a == b);   // *** assertTrue was not resolving

我确实让它工作了:

org.junit.Assert.assertTrue(a == b);

很奇怪,为什么 IntelliJ 没有告诉我要在此处导入什么,即使尝试添加,我也无法正常工作:import org.junit.Assert;

【问题讨论】:

  • mvn 需要一个目标。你可能是想运行mvn package 缺少版本的东西只是一个警告。如果您不指定版本,它将使用可用的最新版本。这可能会在未来引起问题,但可能无需担心。

标签: java maven intellij-idea


【解决方案1】:

要摆脱烦人的版本警告,请先添加它们:-

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        ...
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        ...
    </plugin>
    ...
</plugins>

assertTrue() 没有立即解析的原因是因为您需要先配置 Intellij 以自动解析 Assert 上的“导入静态”。这是一次性的事情。要在 Intellij 中进行配置,请参阅我在 Add favorite methods to static import in IntelliJ? 发布的解决方案。一旦配置好,它会在你输入任何断言语句后很好地解决。您可以在此处添加其他有用的import static,例如,如果您使用的是Hamcrest等。

正如其他人提到的,当您通过命令行运行mvn 命令时,您需要在mvn 之后指定一个目标。如果您在 Intellij 的右侧导航中单击“Maven 项目”,然后展开下拉列表,您将看到您通常调用的所有列出的目标(清理、验证、编译、测试等)通过 Intellij。这是detailed explanation of them

如果我是你,我会直接从 Intellij 运行 maven 目标,而不是通过命令行,但这确实是一种偏好……任何一种方式都有效。

【讨论】:

  • 嗨,我使用的是 Intellij 9。我在对话框中添加了引用,选中了静态复选框,仍然没有做任何事情。那我该如何手动完成呢?导入 org.junit.Assert;甚至不起作用!
  • 好的,现在它可以与 Assert.assertTrue 一起使用导入(手动)。
  • @Blankman,我认为 v9 也应该可以正常工作。当您键入assertTrue 时,您需要在该语句上点击 ALT+ENTER 键...您将弹出“静态导入方法...”。它不会自动为您插入静态导入,这与 Eclipse 非常不同。
  • 我不认为你想做Assert.assertTrue(..),因为虽然它有效,但它往往会用这些前缀混淆你的测试用例。
【解决方案2】:

你不需要运行 mvn

你需要运行类似 mvn compile 或 mvn package 的东西

您似乎还缺少编译器插件和战争插件的版本。你能发布你的 pom.xml 来告诉你在哪里解决这个问题吗?

【讨论】:

    【解决方案3】:

    您必须使用目标调用 maven,例如

    mvn clean
    mvn package
    mvn install
    

    您现在可以忽略缺少版本的警告。但是,您应该将版本添加到您的 pom.xml。

    当你在 Spring 中使用 junit 进行测试时:

    mvn test
    

    这也是一个有效的目标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-23
      • 2011-06-18
      • 2020-09-25
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      相关资源
      最近更新 更多