为什么在Maven中编译命令失败?

huangapple go评论85阅读模式
英文:

Why compile command failed in Maven?

问题

我刚接触Maven,正在尝试通过命令提示符运行。命令mvn build成功执行了清理命令,但在构建编译和测试命令时失败了。
以下是我的命令提示符屏幕截图:

<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>bigProjectName</groupId>
  <artifactId>SubProjectName</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SubProjectName</name>
  <url>http://maven.apache.org</url>
  
  <profiles>
    <profile>
      <id>Regression</id>
    </profile>
  </profiles>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>3.0.0-M5</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>4.0.0-alpha-6</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.3.0</version>
      <scope>test</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
    <dependency>
      <groupId>io.appium</groupId>
      <artifactId>java-client</artifactId>
      <version>7.3.0</version>
    </dependency>
  </dependencies>
</project>

请为我提供建议,我该怎么做?

英文:

I'm new on Maven and I am trying to run through command prompt. Command mvn build successfully clean command. but it failed to build compile and test command.
this is my cmd screen

为什么在Maven中编译命令失败?

    &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    
      &lt;groupId&gt;bigProjectName&lt;/groupId&gt;
      &lt;artifactId&gt;SubProjectName&lt;/artifactId&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;SubProjectName&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
      
      &lt;profiles&gt;
      &lt;profile&gt;
      &lt;id&gt;Regression&lt;/id&gt;
      
      &lt;/profile&gt;
      &lt;/profiles&gt;
      &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;/properties&gt;
    
    &lt;build&gt;
        &lt;pluginManagement&gt;
          &lt;plugins&gt;
            &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
              &lt;version&gt;3.0.0-M5&lt;/version&gt;
            &lt;/plugin&gt;
          &lt;/plugins&gt;
        &lt;/pluginManagement&gt;
      &lt;/build&gt;
      
      &lt;dependencies&gt;
      
      &lt;dependency&gt;
        &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
        &lt;artifactId&gt;selenium-java&lt;/artifactId&gt;
        &lt;version&gt;4.0.0-alpha-6&lt;/version&gt;
      &lt;/dependency&gt;
    
    &lt;!-- https://mvnrepository.com/artifact/org.testng/testng --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;org.testng&lt;/groupId&gt;
        &lt;artifactId&gt;testng&lt;/artifactId&gt;
        &lt;version&gt;7.3.0&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
    &lt;!-- https://mvnrepository.com/artifact/io.appium/java-client --&gt;
      &lt;dependency&gt;
        &lt;groupId&gt;io.appium&lt;/groupId&gt;
        &lt;artifactId&gt;java-client&lt;/artifactId&gt;
        &lt;version&gt;7.3.0&lt;/version&gt;
      &lt;/dependency&gt;
    
      &lt;/dependencies&gt;
    &lt;/project&gt;

Please suggest me what should I do?

答案1

得分: 4

为了指定你想要的 Java 版本,添加两个属性。目前默认版本是 5(已不再受支持)。例如,

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
英文:

Add two properties to specify the version of Java you want to target. Currently, it's defaulting to 5 (which is no longer supported). For example,

&lt;properties&gt;
    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
&lt;/properties&gt;

答案2

得分: 0

&lt;properties&gt;
    &lt;java.version&gt;8&lt;/java.version&gt;
&lt;/properties&gt;

    &lt;build&gt;
        &lt;pluginManagement&gt;
            &lt;plugins&gt;
                &lt;plugin&gt;
                    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                    &lt;version&gt;3.8.0&lt;/version&gt;
                    &lt;configuration&gt;
                        &lt;source&gt;8&lt;/source&gt;
                        &lt;target&gt;8&lt;/target&gt;              
                    &lt;/configuration&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/pluginManagement&gt;
    &lt;/build&gt;

声明 maven-compiler-plugin 是必须的。您可以根据您的 JDK 版本将数字 8(Java 版本)更改为至少 71112 等。

英文:

use something like this

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;com.example&lt;/groupId&gt;
    &lt;artifactId&gt;core01&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;name&gt;foobar&lt;/name&gt;

    &lt;properties&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;java.version&gt;8&lt;/java.version&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;fuu&lt;/groupId&gt;
            &lt;artifactId&gt;bar&lt;/artifactId&gt;
            &lt;version&gt;foo&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
    &lt;build&gt;
        &lt;pluginManagement&gt;
            &lt;plugins&gt;
                &lt;plugin&gt;
                    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                    &lt;version&gt;3.8.0&lt;/version&gt;
                    &lt;configuration&gt;
                        &lt;source&gt;8&lt;/source&gt;
                        &lt;target&gt;8&lt;/target&gt;              
                    &lt;/configuration&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/pluginManagement&gt;
    &lt;/build&gt;
&lt;/project&gt;

Focus at these line for setting Java version of compiler

&lt;properties&gt;
    &lt;java.version&gt;8&lt;/java.version&gt;
&lt;/properties&gt;

and

    &lt;build&gt;
        &lt;pluginManagement&gt;
            &lt;plugins&gt;
                &lt;plugin&gt;
                    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                    &lt;version&gt;3.8.0&lt;/version&gt;
                    &lt;configuration&gt;
                        &lt;source&gt;8&lt;/source&gt;
                        &lt;target&gt;8&lt;/target&gt;              
                    &lt;/configuration&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/pluginManagement&gt;
    &lt;/build&gt;

declaring maven-compiler-plugin is must have. You can change number 8 (Java version) to at least 7 or 11, 12 etc. depend on your JDK version.

huangapple
  • 本文由 发表于 2020年10月4日 10:41:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64190701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定