【问题标题】:How to create web application Maven archetype for Eclipse?如何为 Eclipse 创建 Web 应用程序 Maven 原型?
【发布时间】:2011-07-21 18:00:59
【问题描述】:

我有一个工作的或大部分工作的 POM,它获取所有且完全只是我对编写 Facelets 应用程序感兴趣的 JAR。我正在使用 m2eclipse。当我导入新的 Maven 创建的应用程序时,它干净利落地进入,或多或少符合预期,但是,它不像动态 Web 项目,因为当我尝试运行它时,它没有提供 Run As -> Run on Server选项。

我研究了其他可以在服务器上运行的 Maven 生成的 Eclipse 项目(但没有设置我想要的 JAR 组合)。例如,这个可行,但它没有我想要的 JAR:

mvn 原型:生成-DarchetypeArtifactId=maven-archetype-webapp \ -DgroupId=package-name -DartifactId=project-name

我是否需要创建一个自定义原型 artifactId 来模仿 ...-DarchetypeArtifactId=maven-archetype-webapp... 我在上面的命令中看到,如果需要,如何?

或者它仅仅是该原型设置的文件系统的功能?我看到我生成的项目(webapp1)...

webapp1 |-- 源 | `--主要 | |-- 爪哇 | |-- 资源 | `-- 网络应用 | `-- 网络信息 `-- 目标 `--类`

... 与使用 maven-archetype-webapp 生成的内容不同,但我不知道如何强制 mvn eclipse:clean eclipse:eclipse生成它(或者这是否告诉 Eclipse 这是一个 Web 应用程序,它应该设置为 Run As -> Run on Server)。

简单网络 |-- 源 | `--主要 | |-- 爪哇 | | `--com | | `-- 教程 | |-- 资源 | | `--com | | `-- 教程 | `-- 网络应用 | |-- 元信息 | |-- 页面 | `-- 网络信息 `-- 目标 |-- 班级 | `--com | `-- 教程 |-- maven存档 |-- 元信息 | `-- 行家 | `-- com.mytutorial | `--simpleWeb |-- 页面 |-- 简单网 | |-- 元信息 | |-- 页面 | `-- 网络信息 | |-- 班级 | | `--com | | `-- 教程 | `--库 |-- 万无一失 |-- 测试类 `-- 网络信息 |-- 班级 | `--com | `-- 教程 `--库

非常感谢任何可以让我踏上启蒙之路的人。

罗斯

附:这是我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.etretatlogiciels.webapp1</groupId>
  <artifactId>webapp1</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>webapp1 Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <commons-beanutils-version>    1.8.3        </commons-beanutils-version>
    <commons-codec-version>        1.4          </commons-codec-version>
    <commons-collections-version>  3.2.1        </commons-collections-version>
    <commons-digester-version>     2.1          </commons-digester-version>
    <commons-discovery-version>    0.4          </commons-discovery-version>
    <commons-logging-version>      1.1.1        </commons-logging-version>
    <jsf-facelets-version>         1.1.15       </jsf-facelets-version>
    <myfaces-version>              2.0.4        </myfaces-version>
    <richfaces-version>            3.3.3.Final  </richfaces-version>
  </properties>

  <pluginRepositories>
    <pluginRepository>
      <id>jboss-public-repository-group</id>
      <name>JBoss Public Maven Repository Group</name>
      <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
      <layout>default</layout>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>

  <dependencies>
    <!-- Apache Commons -->
    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>${commons-beanutils-version}</version>
    </dependency>
    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>${commons-codec-version}</version>
    </dependency>
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>${commons-collections-version}</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>commons-digester</groupId>
      <artifactId>commons-digester</artifactId>
      <version>${commons-digester-version}</version>
    </dependency>
    <dependency>
      <groupId>commons-discovery</groupId>
      <artifactId>commons-discovery</artifactId>
      <version>${commons-discovery-version}</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>${commons-logging-version}</version>
    </dependency>

    <!-- Facelets -->
    <dependency>
      <groupId>com.sun.facelets</groupId>
      <artifactId>jsf-facelets</artifactId>
      <version>${jsf-facelets-version}</version>
    </dependency>

    <!-- MyFaces -->
    <dependency>
      <groupId>org.apache.myfaces.core</groupId>
      <artifactId>myfaces-api</artifactId>
      <version>${myfaces-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.myfaces.core</groupId>
      <artifactId>myfaces-impl</artifactId>
      <version>${myfaces-version}</version>
    </dependency>

    <!-- RichFaces -->
    <dependency>
      <groupId>org.richfaces.framework</groupId>
      <artifactId>richfaces-api-jsf2</artifactId>
      <version>${richfaces-version}</version>
    </dependency>
    <dependency>
      <groupId>org.richfaces.framework</groupId>
      <artifactId>richfaces-impl-jsf2</artifactId>
      <version>${richfaces-version}</version>
    </dependency>
    <dependency>
      <groupId>org.richfaces.framework</groupId>
      <artifactId>richfaces-ui-jsf2</artifactId>
      <version>${richfaces-version}</version>
    </dependency>

    <!-- JUnit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>webapp1</finalName>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.java</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
        <source>1.5</source>
        <target>1.5</target>
      </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>richfaces</id>
        <activation>
        <activeByDefault>true</activeByDefault>
        </activation>
        <repositories>
          <repository>
            <id>maven2.repository</id>
            <name>Repository for Maven by Maven</name>
            <url>http://repo2.maven.org/maven2</url>
            <layout>default</layout>
            <releases>
              <enabled>true</enabled>
              <updatePolicy>always</updatePolicy>
            </releases>
          </repository>
          <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/1</url>
            <layout>legacy</layout>
            <releases>
              <enabled>true</enabled>
              <updatePolicy>always</updatePolicy>
            </releases>
          </repository>
          <repository>
            <id>JBoss Repository Old</id>
            <name>Repository for Maven by JBoss</name>
            <url>http://repository.jboss.org/maven2</url>
            <layout>default</layout>
            <releases>
              <enabled>true</enabled>
              <updatePolicy>always</updatePolicy>
            </releases>
          </repository>
          <repository>
            <id>JBoss Repository Nexus</id>
            <name>Repository for Maven by JBoss</name>
            <url>http://repository.jboss.org/nexus/content/groups/public-jboss</url>
            <layout>default</layout>
            <releases>
              <enabled>true</enabled>
              <updatePolicy>always</updatePolicy>
          </releases>
        </repository>
      </repositories>
    </profile>
  </profiles>

</project>

【问题讨论】:

  • 你的目标是什么?你对问题的描述太复杂了。如果你想在 apache tomcat 中运行你的 war,你可以在 eclipse 中添加 apache tomcat 服务器:window->show view->others->server->servers。然后将您的项目链接到 tomcat(我使用文件同步 - 但我的方式不是最简单的)。
  • 谢谢。我想我不知道我想要什么,因为我不知道 Maven 和 Eclipse 之间的空间。我的目标是用理解来连接这个空间,然后为其他人写下它(实际上是创建一个 Web 应用程序)。我的工作区中已经有一个 Tomcat 服务器,但是必须完成通常通过创建动态 Web 项目来处理的 Eclipse 连接。当我让 Maven 创建项目时,这些连线都没有完成。我已经将我的 POM 与其他 POM 进行了比较,这些 POM 的接线似乎可以正常工作并且无法揭示差异。

标签: eclipse maven-2 maven-archetype


【解决方案1】:

我也遇到了同样的问题。现有的原型都不符合我的需求。如果我从自定义原型创建(从 Eclipse)一个项目,它不会成为动态 Web 项目。 如果您运行 mvn -Dwtpversion=2.0 eclipse:eclipse 它会再次添加您不需要的各种东西。 因此,经过调查,我最终发现了什么定义了“动态网络项目”。它是 .settings 目录中的一堆目录和文件以及 .project 中的条目。

所以你需要做的是:
1.) 从 Eclipse 中创建一个空的(非 Maven)动态 Web 项目。
2.) 在不添加任何文件或任何其他更改的情况下,在文件系统上检查 eclipse 创建的以下文件(相对于项目目录):
.project
.settings/org.eclipse.jdt.core.prefs
.settings/org.eclipse.wst.common.project.facet.core.prefs.xml
.settings/org.eclipse.wst.jsdt.ui.superType.name
.settings/org.eclipse.jst.jsp.core.prefs
.settings/org.eclipse.wst.common.project.facet.core.xml
.settings/org.eclipse.wst.common.component
.settings/org.eclipse.wst.jsdt.ui.superType.container

3.) 将这些文件复制到您的 archetype-resources 目录中以用于您的自定义archetime
4.) 编辑并概括复制的文件(将实际项目名称替换为 ${artifactId} 等...
5.) 在您的 archetype-metadata.xml 文件中添加:

<fileSet encoding="UTF-8" filtered="true">
  <directory></directory>
<includes>
  <include>.project</include>
</includes>
</fileSet>
<fileSet encoding="UTF-8" filtered="true">
  <directory>.settings</directory>
</fileSet>

没有这个maven不会上传“.*”开头的文件

6.) 瞧!安装并部署您的原型,然后重试。它对我有用。

【讨论】:

  • 超级!非常感谢,我的朋友!
【解决方案2】:

在你的archetype-resources/pom.xml中配置maven-eclipse-plugin怎么样?

<build>
    <finalName>archetype-javaee5-webapp</finalName>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <wtpversion>2.0</wtpversion>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>    

这将在 Eclipse 中创建一个可用于 Web 的项目

【讨论】:

  • 感谢您的回复。最终,我为 Ivy + ant 放弃了 Maven。我知道在一个完全 Maven 的世界中承认这一点是令人厌恶的,但希望警察永远不会找到我 ;-) 如果我有机会重新做出决定,我会尝试这个解决方案。跨度>
  • 这是我用来在 Eclipse 中识别我的 Web 应用程序的解决方案。按预期工作。
  • 确保安装 Maven Integration for Eclipse WTP (m2e-wtp) marketplace.eclipse.org/node/441371#.URhKr1qje1k
【解决方案3】:

要为 Eclipse 创建一个动态 Web 项目,您需要这样做:

1) 使用类似的东西生成一个项目:

mvn archetype:generate >可选参数,例如 -Dfilter=webapp-j2ee

这将帮助您在交互模式下生成 maven 项目,您无需输入 artifactId、groupId 等选项。

注意:它仍然可能不是一个动态 Web 项目,就我而言,它永远不是...... (我想这基本上取决于您使用的原型-artifactId,我还没有找到一个可以为基本的 j2ee 应用程序自行生成动态 Web 模块的模块,该应用程序将只使用 jsp、servlet 等等)

2) 现在要为 Eclipse 生成项目文件,使用以下命令:

mvn -Dwtpversion=2.0 eclipse:eclipse

这将生成一个项目文件,其中包含所需的项目构面集。因此,当您在 Eclipse IDE 中导入它时,它将是一个“动态 Web 模块”。

注意:现在,如果您正在寻找“动态 Web 模块”的特定版本,那么您需要配置您的 POM。版本取决于构建标签中的以下标签:

<code>
<build>
......
  <plugins>
  ........
    <plugin>
      <groupId>         org.apache.maven.plugins  </groupId>
      <artifactId>      maven-compiler-plugin     </artifactId>
      <version>         2.3.2                     </version>
      <configuration>
                   <source>   1.6     </source>
                   <target>   1.6     </target>
      </configuration>
    </plugin>
  .........
  </plugins>
.......
</build>
</code>

现在,如果源和目标设置为 1.6(如本例所示),则将选择动态 Web 模块 2.5。否则,编译器插件 1.4 等将为 2.4。

【讨论】:

    猜你喜欢
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2013-08-20
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    相关资源
    最近更新 更多