gpt4 book ai didi

java - 如何在我的文件夹结构中的 Maven 中运行一个简单的 hello 词?

转载 作者:行者123 更新时间:2023-11-30 02:32:20 33 4
gpt4 key购买 nike

我正在尝试按照以下结构运行这个 maven Hello Word:

├── pom.xml
└── src
  └── Main.java

使用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>.</groupId>
<artifactId>TestJava</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<!-- https://stackoverflow.com/questions/33124516/can-files-outside-of-the-maven-folder-structure-get-compiled -->
<sourceDirectory>${basedir}/src</sourceDirectory>
<scriptSourceDirectory>${basedir}/src</scriptSourceDirectory>
<testSourceDirectory>${basedir}/src</testSourceDirectory>
<plugins>
<plugin>
<groupId>.</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>Main</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>

对于文件src/Main.java:

public class Main {

public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}

}

但是当我使用 mvn exec:java 运行它时出现错误:

Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T16:39:06-03:00)
Maven home: D:\User\Documents\apache_marven\maven
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_65\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "x86", family: "windows"
...
[INFO] Scanning for projects...
[WARNING] The POM for .:exec-maven-plugin:jar:1.2.1 is missing, no dependency information available
[WARNING] Failed to retrieve plugin descriptor for .:exec-maven-plugin:1.2.1: Plugin .:exec-maven-plugin:1.2.1 or one of its dependencies could not be resolved: Failure to find .:exec-maven-plugin:jar:1.2.1 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestJava 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for .:exec-maven-plugin:jar:1.2.1 is missing, no dependency information available
[WARNING] Failed to retrieve plugin descriptor for .:exec-maven-plugin:1.2.1: Plugin .:exec-maven-plugin:1.2.1 or one of its dependencies could not be resolved: Failure to find .:exec-maven-plugin:jar:1.2.1 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ TestJava ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.114 s
[INFO] Finished at: 2017-05-13T22:38:28-03:00
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project TestJava: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[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/PluginParameterException
<小时/>

更新

使用 @Damian Lattenero 答案给我错误:

 mvn exec:java
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.mycompany.app:my-app:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 92, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building your proyect name 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.3/maven-dependency-plugin-2.3.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.3/maven-dependency-plugin-2.3.pom (11 kB at 4.8 kB/s)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/21/maven-plugins-21.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/21/maven-plugins-21.pom (12 kB at 33 kB/s)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/20/maven-parent-20.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/20/maven-parent-20.pom (25 kB at 58 kB/s)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.3/maven-dependency-plugin-2.3.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.3/maven-dependency-plugin-2.3.jar (132 kB at 201 kB/s)
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ my-app >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ my-app <<<
[INFO]
Downloading: https://repo.maven.apache.org/maven2/org/checkerframework/checker/1.9.4/checker-1.9.4.pom
Downloaded: https://repo.maven.apache.org/maven2/org/checkerframework/checker/1.9.4/checker-1.9.4.pom (4.2 kB at 12 kB/s)
Downloading: https://repo.maven.apache.org/maven2/org/checkerframework/checker-qual/1.9.4/checker-qual-1.9.4.pom
Downloaded: https://repo.maven.apache.org/maven2/org/checkerframework/checker-qual/1.9.4/checker-qual-1.9.4.pom (4.2 kB at 12 kB/s)
Downloading: https://repo.maven.apache.org/maven2/org/checkerframework/jdk8/1.9.4/jdk8-1.9.4.pom
Downloaded: https://repo.maven.apache.org/maven2/org/checkerframework/jdk8/1.9.4/jdk8-1.9.4.pom (4.1 kB at 11 kB/s)
Downloading: https://repo.maven.apache.org/maven2/org/checkerframework/checker/1.9.4/checker-1.9.4.jar
Downloading: https://repo.maven.apache.org/maven2/org/checkerframework/jdk8/1.9.4/jdk8-1.9.4.jar
Downloading: https://repo.maven.apache.org/maven2/org/checkerframework/checker-qual/1.9.4/checker-qual-1.9.4.jar
Downloaded: https://repo.maven.apache.org/maven2/org/checkerframework/jdk8/1.9.4/jdk8-1.9.4.jar (419 kB at 469 kB/s)
Downloaded: https://repo.maven.apache.org/maven2/org/checkerframework/checker-qual/1.9.4/checker-qual-1.9.4.jar (169 kB at 136 kB/s)
Downloaded: https://repo.maven.apache.org/maven2/org/checkerframework/checker/1.9.4/checker-1.9.4.jar (2.3 MB at 667 kB/s)
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ my-app ---
[WARNING]
java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
at java.lang.Thread.run(Thread.java:748)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.827 s
[INFO] Finished at: 2017-05-13T23:10:39-03:00
[INFO] Final Memory: 7M/19M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project my-app: An exception occured while executing the Java class. your package name -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[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/MojoExecutionException
<小时/>

并使用 @Steve C 答案,如下:

<?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>.</groupId>
<artifactId>TestJava</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<!-- https://stackoverflow.com/questions/33124516/can-files-outside-of-the-maven-folder-structure-get-compiled -->
<sourceDirectory>${basedir}/src</sourceDirectory>
<scriptSourceDirectory>${basedir}/src</scriptSourceDirectory>
<testSourceDirectory>${basedir}/src</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>Main</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>

给我错误:

 mvn exec:java
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestJava 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ TestJava >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ TestJava <<<
[INFO]
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ TestJava ---
[WARNING]
java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
at java.lang.Thread.run(Thread.java:748)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.271 s
[INFO] Finished at: 2017-05-13T23:14:27-03:00
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project TestJava: An exception occured while executing the Java class. Main -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[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/MojoExecutionException

最佳答案

您的插件配置不正确:

        <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>Main</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
</configuration>
</plugin>

我不知道您从哪里得到在任何地方使用 . 作为 groupId 的想法,但我怀疑这不是一个好主意。

接下来,仅执行 mvn exec:java 不会编译您的项目 - 它只是尝试执行它。

尝试:

mvn package exec:java

它将构建一个包含您的类的 jar 文件,然后尝试运行它。

您可能会遇到进一步的问题,因为您的类(class)尚未放置在package structure中.

关于java - 如何在我的文件夹结构中的 Maven 中运行一个简单的 hello 词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43959623/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com