【问题标题】:Java EE eclipse project directory structure?Java EE eclipse项目目录结构?
【发布时间】:2023-04-04 21:45:01
【问题描述】:

我正在尝试使用 Java EE、Spring 和 Maven(使用 Nexus 存储库管理器)在 Eclipse 中设置示例动态 Web 项目。我想知道是否有人知道我应该在 Eclipse 中为企业 Web 应用程序设置的“最佳实践”目录结构?我应该坚持为我设置的默认结构吗?我问是因为环顾互联网,我看到了很大的变化(例如,WEB-INF 和 META-INF 文件夹在哪里......如果有一个“战争”目录等)。谢谢!

【问题讨论】:

标签: eclipse maven-2 jakarta-ee


【解决方案1】:

如果您使用 Maven,我强烈建议您遵循 Maven 的约定。这是 Maven 世界中的“最佳实践”(我看不出有什么好的理由不这样做,不遵循这个建议会导致更多的工作)。

创建 webapp 项目的一种简单方法是使用 maven-archetype-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

此布局符合 Eclipse WTP(无论您使用什么插件来进行 Eclipse 集成)。只需将此项目导入 Eclipse 即可。

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

【讨论】:

  • 谢谢!您能否详细说明使用“maven-archetype-webapp”。这是我可以用来自动生成这个目录结构的工具吗?
  • 是的,原型是“maven 项目生成器”。实际上,我只是在提到它之后编写了命令并显示了获得的结构。我认为这会很清楚。
  • 我从 Apache 发布的链接解释了整个目录结构标准。吉姆,你读了吗?
  • 另请注意,您可以使用“mvn archetype:generate”而不需要进一步的参数来获取生成原型所需的其余信息的交互式提示。
【解决方案2】:

如果您使用 Maven,最好关注 their convention

如果您使用的是 Spring,则不需要 EAR。 WAR 就可以了。

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 文件作为输出。

我有一个通常遵循 IntelliJ 目录结构的 Ant build.xml。如果觉得有用,欢迎转发。

<?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 强加给你一个;我碰巧不欣赏这一点。 WAR 和 EAR 文件有一个明确的固定要求,但只要您可以生成,您就可以以任何对您有意义的方式进行操作。
  • 如果你愿意,当然可以使用 Maven 并覆盖他们的默认方案
  • 感谢您的更新。因此,如果我使用 Maven 和 Spring,那么最好遵循您链接到的 Maven 约定?
  • 基本上我最初很困惑,因为感觉项目的所有组件(包括 Eclipse IDE)都在竞争“自己的”目录结构。也许我错了,但似乎最终必须手动覆盖一个组件以符合某种约定。