JavaEEEclipse项目目录结构?

JavaEEEclipse项目目录结构?,eclipse,maven-2,jakarta-ee,Eclipse,Maven 2,Jakarta Ee,我正在尝试使用JavaEE、Spring和Maven(使用NexusRepositoryManager)在Eclipse中设置一个示例动态web项目。我想知道是否有人知道我应该在Eclipse中为企业web应用程序设置的“最佳实践”目录结构?我应该坚持为我设置的默认结构吗?我这样问是因为环顾互联网,我看到了很大的变化(例如,WEB-INF和META-INF文件夹的位置……如果有“war”目录等)。谢谢 如果您使用的是Maven,最好遵循 如果使用Spring,则不需要耳朵。战争就可以了 WAR文

我正在尝试使用JavaEE、Spring和Maven(使用NexusRepositoryManager)在Eclipse中设置一个示例动态web项目。我想知道是否有人知道我应该在Eclipse中为企业web应用程序设置的“最佳实践”目录结构?我应该坚持为我设置的默认结构吗?我这样问是因为环顾互联网,我看到了很大的变化(例如,WEB-INF和META-INF文件夹的位置……如果有“war”目录等)。谢谢

如果您使用的是Maven,最好遵循

如果使用Spring,则不需要耳朵。战争就可以了

WAR文件有一个您必须遵循的明确标准。只要能够生成正确的WAR文件,就可以对源代码使用任何对您有意义的目录结构

我用这样的方式:

/project
+---/src (.java)
+---/test (TestNG .java here)
+---/test-lib (testNG JAR, Spring test JAR, etc.)
+---/resources (log4j.xml, etc.)
+---/web (root of web content here)
+---+---/WEB-INF
+---+---+---/classes (compile .java to this directory)
+---+---+---/lib (JAR files)
我使用IntelliJ,所以它会创建一个分解的WAR文件作为输出

我有一个Ant build.xml,它通常遵循IntelliJ目录结构。如果你觉得有用的话,欢迎你把它抄下来

<?xml version="1.0" encoding="UTF-8"?>
<project name="xslt-converter" basedir="." default="package">

    <property name="version" value="1.6"/>
    <property name="haltonfailure" value="no"/>

    <property name="out" value="out"/>

    <property name="production.src" value="src"/>
    <property name="production.lib" value="lib"/>
    <property name="production.resources" value="config"/>
    <property name="production.classes" value="${out}/production/${ant.project.name}"/>

    <property name="test.src" value="test"/>
    <property name="test.lib" value="lib"/>
    <property name="test.resources" value="config"/>
    <property name="test.classes" value="${out}/test/${ant.project.name}"/>

    <property name="exploded" value="out/exploded/${ant.project.name}"/>
    <property name="exploded.classes" value="${exploded}/WEB-INF/classes"/>
    <property name="exploded.lib" value="${exploded}/WEB-INF/lib"/>

    <property name="reports.out" value="${out}/reports"/>
    <property name="junit.out" value="${reports.out}/junit"/>
    <property name="testng.out" value="${reports.out}/testng"/>

    <path id="production.class.path">
        <pathelement location="${production.classes}"/>
        <pathelement location="${production.resources}"/>
        <fileset dir="${production.lib}">
            <include name="**/*.jar"/>
            <exclude name="**/junit*.jar"/>
            <exclude name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="test.class.path">                            
        <path refid="production.class.path"/>
        <pathelement location="${test.classes}"/>
        <pathelement location="${test.resources}"/>
        <fileset dir="${test.lib}">
            <include name="**/junit*.jar"/>
            <include name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="testng.class.path">
        <fileset dir="${test.lib}">
            <include name="**/testng*.jar"/>
        </fileset>
    </path>

    <available file="${out}" property="outputExists"/>

    <target name="clean" description="remove all generated artifacts" if="outputExists">
        <delete dir="${out}" includeEmptyDirs="true"/>
        <delete dir="${reports.out}" includeEmptyDirs="true"/>
    </target>

    <target name="create" description="create the output directories" unless="outputExists">
        <mkdir dir="${production.classes}"/>
        <mkdir dir="${test.classes}"/>
        <mkdir dir="${reports.out}"/>
        <mkdir dir="${junit.out}"/>
        <mkdir dir="${testng.out}"/>
        <mkdir dir="${exploded.classes}"/>
        <mkdir dir="${exploded.lib}"/>
    </target>

    <target name="compile" description="compile all .java source files" depends="create">
        <!-- Debug output
                <property name="production.class.path" refid="production.class.path"/>
                <echo message="${production.class.path}"/>
        -->
        <javac srcdir="src" destdir="${out}/production/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="production.class.path"/>
            <include name="**/*.java"/>
            <exclude name="**/*Test.java"/>
        </javac>
        <javac srcdir="${test.src}" destdir="${out}/test/${ant.project.name}" debug="on" source="${version}">
            <classpath refid="test.class.path"/>
            <include name="**/*Test.java"/>
        </javac>
    </target>

    <target name="junit-test" description="run all junit tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <junit printsummary="yes" haltonfailure="${haltonfailure}">
            <classpath refid="test.class.path"/>
            <formatter type="xml"/>
            <batchtest fork="yes" todir="${junit.out}">
                <fileset dir="${test.src}">
                    <include name="**/*Test.java"/>
                </fileset>
            </batchtest>
        </junit>
        <junitreport todir="${junit.out}">
            <fileset dir="${junit.out}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report todir="${junit.out}" format="frames"/>
        </junitreport>
    </target>

    <taskdef resource="testngtasks" classpathref="testng.class.path"/>
    <target name="testng-test" description="run all testng tests" depends="compile">
        <!-- Debug output
                <property name="test.class.path" refid="test.class.path"/>
                <echo message="${test.class.path}"/>
        -->
        <testng classpathref="test.class.path" outputDir="${testng.out}" haltOnFailure="${haltonfailure}" verbose="2" parallel="methods" threadcount="50">
            <classfileset dir="${out}/test/${ant.project.name}" includes="**/*.class"/>
        </testng>
    </target>

    <target name="exploded" description="create exploded deployment" depends="testng-test">
        <copy todir="${exploded.classes}">
            <fileset dir="${production.classes}"/>
        </copy>
        <copy todir="${exploded.lib}">
            <fileset dir="${production.lib}"/>
        </copy>
    </target>

    <target name="package" description="create package file" depends="exploded">
        <jar destfile="${out}/${ant.project.name}.jar" basedir="${production.classes}" includes="**/*.class"/>
    </target>

</project>

如果您使用Maven,我强烈建议您遵循Maven的惯例。这是Maven世界中的“最佳实践”(我不认为有任何理由不这样做,不遵循这一建议会导致更多的工作)

创建webapp项目的一种简单方法是使用maven原型webapp:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp \
                       -DgroupId=com.mycompany.app \
                       -DartifactId=my-webapp \
                       -Dversion=1.0-SNAPSHOT 
(您可以在Linux shell中“按原样”粘贴此命令;在Windows上,在单行中键入所有内容,而不使用
“\”

这就是你将得到的结构:

my-webapp
|-- pom.xml
`-- src
    `-- main
        |-- resources
        `-- webapp
            |-- WEB-INF
            |   `-- web.xml
            `-- index.jsp
此布局符合EclipseWTP(用于Eclipse集成的任何插件)。只要将这个项目导入Eclipse,就可以了


如果您有更具体的问题,请随时提问(例如,大多数时候您不必担心
META-INF
目录,但如果确实需要,请将其放在
src/main/resources
下)。

感谢您的回复。那么,你会说真的没有一个正确的或“最佳实践”的结构吗?没有普遍的共识。Maven强加给你一个;我并不欣赏这一点。WAR和EAR文件有一个明确的固定要求,但只要你能生成它,你就可以用任何对你有意义的方式来做。当然,你可以使用Maven并覆盖他们的默认方案,如果你愿意的话。因此,如果我使用Maven和Spring,那么遵循您链接到的Maven约定将是一个好主意?基本上,我最初感到困惑,因为感觉项目的所有组件(包括Eclipse IDE)都在争夺“它们自己的”目录结构。也许我错了,但似乎一个组件最终必须手动重写才能符合特定的约定。谢谢!你能详细介绍一下“maven原型webapp”的使用方法吗。这是我可以用来自动生成这个目录结构的工具吗?是的,原型是“maven项目生成器”。事实上,我在提到它之后写了这个命令,并展示了获得的结构。我想这应该很清楚,我从Apache发布的链接解释了整个目录结构标准。Jim,你读过了吗?还请注意,你可以使用“mvn archetype:generate”而无需进一步的参数来获取生成原型所需的其他信息的交互式提示。为了让Eclipse和maven更好地发挥作用,我建议下载m2eclipse插件,它的更新站点是