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?

本秂侑毒 提交于 2021-02-11 15:10:32

问题


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.

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.


回答1:


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.

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.

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.

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

Clean option: might not be possible

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

Dirty but reliable option

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>

Hope that helps.



来源:https://stackoverflow.com/questions/26812967/why-do-i-need-to-set-target-folder-as-source-folder-how-was-the-project-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!