【问题标题】:Custom unit test folder/path doesn't see custom src folder/path in Maven project when using terminal使用终端时,自定义单元测试文件夹/路径在 Maven 项目中看不到自定义 src 文件夹/路径
【发布时间】:2019-01-09 23:53:58
【问题描述】:

我的项目中有一个自定义结构

Development -
            -  src
            -  test

并且通过 Eclipse 运行测试工作正常,但是当我使用我的 mac 上的终端运行测试(mvn 测试)时,找不到 src 类(在 maven 编译插件期间失败)。我认为这一定是我的 pom 没有将插件引导到正确的文件夹的问题。如果我注释掉这一行,

 <outputDirectory>Development/build</outputDirectory>

它工作正常,因为 .class 文件现在被放置在目标文件夹中。我不需要查看 .class 文件的目标文件夹,而是需要 maven 来查看

Development/build

目录,如果我没记错的话。对吧?

我想继续使用我的自定义文件结构并更正我的 pom,以便它通过我的 mac 终端编译和运行单元测试。

<build>
   <sourceDirectory>Development/src/main</sourceDirectory>
   <testSourceDirectory>Development/src/test</testSourceDirectory>
   <plugins>
      <plugin>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>3.5.1</version>
         <configuration>
            <source>1.7</source>
            <target>1.7</target>
            <outputDirectory>Development/build</outputDirectory>
         </configuration>
      </plugin>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <version>2.3.1</version>
         <configuration>
            <classesDirectory>Development/build</classesDirectory>
         </configuration>
      </plugin>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-clean-plugin</artifactId>
         <version>3.0.0</version>
         <executions>
            <execution>
               <id>auto-clean</id>
               <phase>clean</phase>
               <goals>
                  <goal>clean</goal>
               </goals>
               <configuration>
                  <filesets>
                     <fileset>
                        <directory>Development/build</directory>
                     </fileset>
                  </filesets>
               </configuration>
            </execution>
         </executions>
      </plugin>
      <plugin>
         <artifactId>maven-assembly-plugin</artifactId>
         <version>3.1.0</version>
         <configuration>
            <descriptors>
               <descriptor>Development/src/assembly/assembly.xml</descriptor>
            </descriptors>
            <tarLongFileMode>posix</tarLongFileMode>
            <executions>
               <execution>
                  <phase>package</phase>
                  <goals>
                     <goal>single</goal>
                  </goals>
               </execution>
            </executions>
         </configuration>
      </plugin>
   </plugins>
</build>

这是控制台输出

[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ weatherdetails ---
[INFO] Deleting /Users/username/git/weatherdetails/target
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (auto-clean) @ weatherdetails ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ weatherdetails ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/username/git/weatherdetails/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ weatherdetails ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2503 source files to /Users/username/git/weatherdetails/Development/build
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ weatherdetails ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/username/git/weatherdetails/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ weatherdetails ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/username/git/weatherdetails/Development/build
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/username/git/weatherdetails/Development/src/test/com/wfc/schedulers/WeeklySchedulerTest.java:[12,26] cannot find symbol
  symbol:   class WeeklyScheduler
  location: package com.wfc.schedulers

关于未正确配置的任何建议?

【问题讨论】:

  • 遵循配置范式的约定,不要更改sourceDirectory&gt;Development/src/main&lt;/sourceDirectory&gt;之类的东西,因为它没有任何优势......
  • 我今天实际上和我的领导谈过这个问题。我说使用maven的重点是它的文件夹结构是标准的。我们将不得不告诉我们使用的每一项技术,以在 Development/builder 文件夹中查找所有内容,如果您问我,这在某种程度上违背了目的。
  • 这东西叫做约定优于配置范式。设置的结果很简单,您需要进行许多配置更改,如果您遵循默认设置(特别是文件夹结构),则不需要这些更改......这使其更易于使用,也便于其他人使用。这样可以节省时间和金钱...

标签: eclipse maven terminal java-7


【解决方案1】:

我在发表这篇文章后不久就能完成这项工作,但想看看是否有人有不同的答案。在格式化这篇文章以满足 stackoverflow 标准时,我注意到编译器插件正在将类写入 /Users/username/git/weatherdetails/Development/build。我说等等那是错误的。它应该将它们写入 /Users/username/git/weatherdetails/Development/build/classes 和 /Users/username/git/weatherdetails/Development/build/test-classes。我用谷歌搜索了如何更改 pom 中的目标目录并遇到了这个标签

<directory>Development/build</directory>

我将它添加到构建中并删除了

<outputDirectory>Development/build</outputDirectory>

来自编译器插件的标签,因为我将它添加到那里,认为它正在路径中创建自定义目标文件夹。运行它并且它起作用了。所以现在我的构建看起来像这样

<build>
    <sourceDirectory>Development/src/main</sourceDirectory>
    <testSourceDirectory>Development/src/test</testSourceDirectory>
        <!-- if you put just Development, it will erase everything in the development folder -->
        <directory>Development/build</directory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
        <classesDirectory>Development/build</classesDirectory>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>auto-clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                        <configuration>
                            <filesets>
                                <fileset>
                    <directory>Development/build</directory>
                                </fileset>
                            </filesets>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <descriptors>       <descriptor>Development/src/assembly/assembly.xml</descriptor>
                    </descriptors>
                    <tarLongFileMode>posix</tarLongFileMode>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </configuration>
            </plugin>
        </plugins>
    </build>

希望这对某人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多