开发者

src/main/webapp directory not recognized by Eclipse

I use m2eclipse to import Maven Java projects in Eclipse.

It fails to recognize src/main/webapp as a source directo开发者_如何学JAVAry.

Graphically in the package explorer (or when I look into Java-Build-Path in the project's properties),

this directory isn't in the list of sources folder (while src/main/java or src/main/resources do).

To access it, I have to look directly into the src/ directory, and start unfolding... Not very convenient!

However, if I run maven install, the resources are copied to the correct directory.

(example : src/main/webapp/index.jsp to target/mywar/index.jsp)

Questions

  1. Is this a correct behavior ? (I was thinking it could be treated as other resources ...)
  2. Could I fix it?
  3. I wonder if there are other drawbacks for this situation, that I would not be aware of right now?


  1. Is this a correct behavior ? (I was thinking it could be treated as other resources ...)
  2. Could I fix it?
  3. I wonder if there are other drawbacks for this situation, that I would not be aware of right now?
  1. Yes, to me this is the correct behavior.
  2. It doesn't really make sense for src/main/webapp to be a source folder, it doesn't contain compilable source files.
  3. I don't know. I guess it depends on your expectations :)

That said, m2eclipse made a contribution that allows to make src/main/webapp available at the top level with a specific "Web Resources" label, something like this:

src/main/webapp directory not recognized by Eclipse

This could be a solution for your concern (the not convenient folding).


Instead of adding /src/main/webapp as a source folder in the java build path, add it as a folder to include in the deployment assembly:

MyProject -> properties -> Deployment Assembly: Add "/src/main/webapp" deploys to "/"


As far as maven is concerned, src/main/webapp isn't a source folder in the sense that it's contents aren't compiled / copied to target/classes, so from m2eclipse's point of view, this is correct behaviour. Is there a particular reason you need src/main/webapp to be marked as an Eclipse source folder?


Well you just have to mark the src/main/webapp as an Eclipse src folder:

right click webapp folder --> build path --> use as source folder

Don't think you can easily change this behavior without any ugly hacks, the src marking is just something Eclipse specific that it uses to configure its classpath etc.


In my case, Eclipse failed to recognise src/main/webapp as the Web Resources folder. I resolved it by:

  1. Deleting the project
  2. Restarting eclipse
  3. Taking a fresh update of the Maven project form the SVN.


Many answers claim that it doesn't make sens to have src/main/webapp as a source folder because JSP files are compiled by the container and not maven during build cycle.

However, it does make sense when you're doing security testing:

Some security scanning tools provide plugins that can be integrated into IDEs so that development teams can locally scan their code and fix basic vulnerabilities on their machine, thus reducing the amount of work for security teams that can therefore work on more advanced testing.

The fact that Eclipse excludes this specific directory is a huge problem: by doing that, security tools won't include by default the webapp/ directory into the scan process, which results in false negatives (actual vulnerabilities that aren't reported by the tool). This behavior then leads development teams into thinking that their JSP pages (for example) are safe from a security point of view.

If security testing teams don't pay attention to the scans conducted by development teams (sometimes we can't even check which scans have been performed locally), the product is more likely to be released with vulnerabilities in code contained in the webapp/ folder.

Thus, this behavior doesn't make sense from a security point of view, and people claiming it does are missing some points.

In any case, I solved this problem by adding the src/main/webapp/ folder to the build path by right clicking it in the package explorer --> build path --> use as source folder


One solution is to exploit m2e support for build helper maven plugin and declare src/main/webapp as an additioanl resource path. This also makes eclipse copy saved resources to the build target folder structure.

        <!-- help eclipse identify the webapp folder as a resource folder: -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/webapp</directory>
                                <targetPath>${project.build.directory}/${project.build.finalName}</targetPath>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>


I can think of a reason.

I'm using Maven for developing a GWT project in Eclipse. In my configuration, the src/main/webapp directory contains the HTML and CSS files that I have to refer to as part of my development effort, even though they're not "compilable" files.

Being able to get to them directly as opposed to diving into the src/ folder is a plus. And defining it in the POM so that it gets set up that way means I don't have to manually keep adding the folder every time I run eclipse:eclipse.


To Nestor's point, no kidding. It's an end-to-end development environment and not just a compiler. It's intended as an interface for all source artifacts contributing to a build from JSP's to build configuration such as POM files. Why would eclipse even bother having XML and JSP editors if these aren't compile-able? --Which by the way, are super-useful.

The maven plugin for eclipse (or vice-versa) is terrific because it builds the .classpath, .project, and other configuration needed to immediately begin working with maven projects in eclipse. However, each time the eclipse config is generated (i.e. mvn eclipse:eclipse), unsupported directories must be added manually. That's a major suck.

The fact that these plugins neglect the ability to easily add new source folders viewable from within the IDE is a mystery to me. One maven/eclipse plugin project page even cites work-arounds for making these sources temporarily available by switching the project types and fooling the plugin--of course, you need to change it back immediately or corrupt the build.

Why don't they just create the ability to independently control the source declarations in .classpath???


I found a whole different reason for the same problem I was facing - I would not see src/main/webapp in the package explorer.

Turned out it had nothing to do with either POM or with Eclipse (using SpringSource Suite 3.5.0) being able to load up maven project.

The problem was that inside my src/main/webapp/Scripts/ folder there were .git and .gitignore files. I suspect that may be Eclipse was not able to deal with file names begining with a "." in the webapp folder. I tested this theory across multiple projects and it seems to hold ground so far.

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜