为什么需要将目标文件夹设置为源文件夹?当“目标"成为目标时,项目是如何第一次编译的不疯狂吗? [英] Why do I need to set target folder as source folder? How was the project compiled for the first time when "target" wasn't cretaed?

查看:101
本文介绍了为什么需要将目标文件夹设置为源文件夹?当“目标"成为目标时,项目是如何第一次编译的不疯狂吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的maven项目(使用的IDE:Sprint测试工具),并且关于它的帮助非常有限.我是Maven,Java和其他东西的新手(我了解基本的Java,并且已经在Java中实现了自动化,但是还没有在成熟的Java项目中工作过),我必须自己或多或少地了解我们所有的项目.我试图编译该程序包,但在main \ src \ java中显示以下类型的代码无法解析消息"的错误:消息..我在文件夹中进行搜索,发现只有一个Messages.java文件(包含该方法的定义)在项目中目标"文件夹的子文件夹中.因此,要求我将该子文件夹的路径/文件夹添加为源文件夹,并且错误消失了.

I have an existing maven project (IDE used: Sprint Test Tool) and I have very very limited help available around it. I'm new to maven, java and stuff (I know basic java and have done automation in java but haven't worked on full-fledged java projects) and I have to figure our all of this project more or less by myself. I was trying to compile the package but it shows errors in main\src\java that "Messages can NOT be resolved" over some code of this sort: Messages.. I searched through the folders and found there's just one Messages.java file (with that method definition in it) in sub-folders of the "target" folder in the project. So, I was asked to add the path/folder of this sub-folder as source folder and the error was gone.

我相信,在编译/打包Maven项目时会创建整个目标".并且,将路径添加到源文件夹会为该路径编制索引,并使该路径可用于整理/打包.因此,我想知道项目是如何首先被编译和打包的.我确实在src \ main \ resources \ folders .. \ Messages.properties中看到了Messages.properties,并且Messages.java中定义的方法似乎与此属性文件有一定关系.

I believe whole of "target" is created when a maven project is compiled/packaged. And, adding a path to the source folder indexes the path and makes it available for compliation/packaging. So, I'm wondering how the project was compiled and packaged at the first place. I do see Messages.properties in src\main\resources\folders..\Messages.properties and the methods defined in Messages.java seem to have some relationship with this properties file.

我在网上搜索,但找不到答案.堆叠可以在这里帮助我吗?预先感谢.

I searched through net but couldn't find an answer. Could stack please help me here? Thanks in advance.

推荐答案

您提供的信息有些稀疏,因此如果您可以提供pom.xml文件的摘要(特别是< plugins> 部分,以及项目从顶层开始的目录结构,尤其是此 Messages 文件的位置.

The info you've provided is somewhat sparse, so it will help if you can supply a snippet of your pom.xml file - specifically the <plugins> section, and the directory structure of your project from the top level, especially the location of this Messages files.

您正确的认为 target 目录是由Maven动态创建的,用于存储构建的输出,因此,其内容中的任何内容都不应该是构建的依赖项.

You are correct that the target dir is created dynamically by Maven to store the output of your build, and therefore nothing in its contents should be a dependency of your build.

最可能的情况是,您的构建在 compile 阶段之前运行了某种代码生成器(也许是Ant任务或某些Maven插件),这就是生成 Messages.java 文件.

The most likely case is that your build has some sort of code generators (perhaps an Ant task or some Maven plugin) running prior to the compile phase, and that is what is generating the Messages.java file on which you depend.

假设已生成 Messages.java ,那么解决方案是将其放在 src/main/gen/目录中,并将该目录添加到您的构建路径中(并删除目标).

Assuming that Messages.java is generated, then the solution is to have it sit in src/main/gen/ directory and add that dir to your build path (and remove target).

清洁选项:不可能

如果您可以找出哪个插件正在创建 Messages.java ,并且可以修改其输出路径,那么这是最简单的选项-重新配置它以写入 src/main/gen/而不是 target/.

If you can figure out which plugin is creating Messages.java, and if it's possible to modify its output path, then that's the simplest option - reconfigure it to write to src/main/gen/ instead of target/.

肮脏但可靠的选项

以上原因可能出于任何原因都不可行.然后只需在 pre-compile 期间将 Messages.java 文件从 target/复制到 src/main/gen/>进入您的pom文件:

It may be that the above is not feasible for whatever reason. Then simply copy the Messages.java file from target/ to src/main/gen/ during the pre-compile phase in your pom file:

<plugin>
    <groupId>mojo.codehaus.org</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <executable>cp</executable>
        <arguments>
            <argument>target/Messages.java</argument>
            <argument>src/main/gen/Messages.java</argument>
        </arguments>
    </configuration>
</plugin>

希望有帮助.

这篇关于为什么需要将目标文件夹设置为源文件夹?当“目标"成为目标时,项目是如何第一次编译的不疯狂吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆