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

时间:2017-05-14 01:46:04

标签: java maven

我正在尝试按照结构运行这个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

更新

使用live demo here回答它会给我一个错误:

 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

使用@Damian Lattenero回答,如:

<?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

2 个答案:

答案 0 :(得分:1)

将你的pom更改为并确保你的pacjage结构在pom中匹配:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>your proyect name</name>
  <url>http://maven.apache.org</url>

  <properties>
      <!-- This property will be set by the Maven Dependency plugin -->
      <annotatedJdk>${org.checkerframework:jdk8:jar}</annotatedJdk>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.checkerframework</groupId>
        <artifactId>checker</artifactId>
        <version>1.9.4</version>
    </dependency>
    <dependency>
        <groupId>org.checkerframework</groupId>
        <artifactId>checker-qual</artifactId>
        <version>1.9.4</version>
    </dependency>
    <dependency>
        <groupId>org.checkerframework</groupId>
        <artifactId>jdk8</artifactId>
        <version>1.9.4</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
      <execution>
        <goals>
          <goal>java</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <mainClass>your package name</mainClass>
      <arguments>
        <argument>argument1</argument>
      </arguments>
    </configuration>
      </plugin>
      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
          <mainClass>Your main class</mainClass>
        </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
      </plugin>
      <plugin>
          <!-- This plugin will set the properties values using dependency information -->
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.3</version>
          <executions>
              <execution>
                  <goals>
                      <goal>properties</goal>
                  </goals>
              </execution>
          </executions>
      </plugin>
      <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <fork>true</fork>
              <!-- Add all the checkers you want to enable here -->
              <annotationProcessors>
                  <annotationProcessor>org.checkerframework.checker.nullness.NullnessChecker</annotationProcessor>
              </annotationProcessors>
              <compilerArgs>
                  <!-- location of the annotated JDK, which comes from a Maven dependency -->
                  <arg>-Xbootclasspath/p:${annotatedJdk}</arg>
              </compilerArgs>
          </configuration>
      </plugin>
    </plugins>
  </build>
</project>

查看结构文件here

答案 1 :(得分:1)

您的插件配置不正确:

        <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>

我不知道你在哪里有.作为一个群体使用的概念,但我怀疑这不是一个好主意。

在此之后,仅执行mvn exec:java不会编译您的项目 - 它只是尝试执行它。

尝试:

mvn package exec:java

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

您可能会遇到更多问题,因为您的课程尚未放入package structure