1

我从 github 得到一个 java servlet 项目,它使用 mvn 编译,并使用 jetty 作为 servlet 容器。由于我从未使用过 mvn,所以我遇到了很多问题/。

问题1:当我运行 mvn install 时,它说“BUILD SUCCESS”,但是我将此项目导入eclipse后,导入的许多包无法被eclipse解决。为什么?似乎当我运行“mvn install”时,mvn 已经为我下载了所有依赖项。

问题 2:如何将我的项目部署到码头,然后运行所有 jUnit 测试用例?

问题 3:当我运行“mvn jetty:run”时,它说:

No plugin found for prefix 'jetty' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/root/.m2/repository), central (http://repo.maven.apache.org/maven2)] -> [Help 1]

谷歌说我应该在 mvn 配置中添加码头插件。但是我对项目感到困惑。为什么项目开发人员不将其添加到 pom.xml 中?或者,是否存在其他解决方案?

下面是简单的项目目录。项目名称是http-request。

[root@localhost http-request]# ls
lib  pom.xml  README.md
[root@localhost http-request]# cd lib
[root@localhost lib]# ls
pom.xml  src  target

http-request 下的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.github.kevinsawicki</groupId>
  <artifactId>http-request-parent</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
  <modules>
    <module>lib</module>
  </modules>
</project>

http-request/lib 下的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.sonatype.oss</groupId>
    <artifactId>oss-parent</artifactId>
    <version>7</version>
  </parent>

  <groupId>com.github.kevinsawicki</groupId>
  <artifactId>http-request</artifactId>
  <version>5.5-SNAPSHOT</version>
  <url>https://github.com/kevinsawicki/http-request</url>
  <description>Library for making HTTP requests</description>
  <inceptionYear>2011</inceptionYear>

  <issueManagement>
    <url>https://github.com/kevinsawicki/http-request/issues</url>
    <system>GitHub Issues</system>
  </issueManagement>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jetty.version>8.1.9.v20130131</jetty.version>
  </properties>

  <licenses>
    <license>
      <name>MIT License</name>
      <url>http://www.opensource.org/licenses/mit-license.php</url>
      <distribution>repo</distribution>
    </license>
  </licenses>

  <scm>
    <url>https://github.com/kevinsawicki/http-request</url>
    <connection>scm:git:git://github.com/kevinsawicki/http-request.git</connection>
    <developerConnection>scm:git:git@github.com:kevinsawicki/http-request.git</developerConnection>
  </scm>

  <developers>
    <developer>
      <email>kevinsawicki@gmail.com</email>
      <name>Kevin Sawicki</name>
      <url>https://github.com/kevinsawicki</url>
      <id>kevinsawicki</id>
    </developer>
  </developers>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <archive>
            <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>2.3.7</version>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Require-Bundle />
            <Export-Package>!.,com.github.kevinsawicki.http</Export-Package>
            <Bundle-RequiredExecutionEnvironment>J2SE-1.5</Bundle-RequiredExecutionEnvironment>
          </instructions>
        </configuration>
        <executions>
          <execution>
            <id>bundle-manifest</id>
            <phase>process-classes</phase>
            <goals>
              <goal>manifest</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.8</version>
        <executions>
          <execution>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.1.2</version>
        <executions>
          <execution>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>com.github.github</groupId>
        <artifactId>site-maven-plugin</artifactId>
        <version>0.8</version>
        <configuration>
          <message>Generated site for ${project.name} ${project.version}</message>
          <noJekyll>true</noJekyll>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>site</goal>
            </goals>
            <phase>site</phase>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <reportPlugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
              <version>2.2</version>
              <configuration>
                <dependencyDetailsEnabled>true</dependencyDetailsEnabled>
                <dependencyLocationsEnabled>true</dependencyLocationsEnabled>
              </configuration>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-javadoc-plugin</artifactId>
              <version>2.8</version>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-report-plugin</artifactId>
              <version>2.6</version>
            </plugin>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-changelog-plugin</artifactId>
              <version>2.2</version>
            </plugin>
            <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>emma-maven-plugin</artifactId>
              <version>1.0-alpha-3</version>
            </plugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>sign</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>1.4</version>
            <executions>
              <execution>
                <id>sign-artifacts</id>
                <phase>verify</phase>
                <goals>
                  <goal>sign</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${jetty.version}</version>
      <scope>test</scope>
    </dependency>
    <!-- Used to test proxy -->
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-servlet</artifactId>
      <version>${jetty.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-servlets</artifactId>
      <version>${jetty.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
4

1 回答 1

0

提出了几个问题。让我分开我的答案。

以 root 身份运行构建

我强烈建议您停止以 root 身份运行构建。

[root@localhost http-request]# ls
lib  pom.xml  README.md
[root@localhost http-request]# cd lib
[root@localhost lib]# ls
pom.xml  src  target

这是一种危险的做法。在您的系统上创建一个普通用户帐户并使用它。

Eclipse 中缺少依赖项

我已经测试了您正在构建的项目并确认它的构建如下:

git clone https://github.com/kevinsawicki/http-request.git
cd http-request
mvn install

您说从 Eclipse 中运行时它会失败?这可能是因为您没有为 Maven 安装 Eclipse 插件吗?请参阅以下问题:

找不到前缀“jetty”的插件

抛出此错误是因为尚未将构建配置为使用码头。您需要阅读有关如何在构建中启用此功能的文档

你的问题在这里:

谷歌说我应该在 mvn 配置中添加码头插件。但是我对项目感到困惑。为什么项目开发人员不将其添加到 pom.xml 中?或者,是否存在其他解决方案?

需要是项目开发人员的地址。最可能的解释是他没有使用 Jetty 来测试他的代码。例如,在我的项目中,我使用一个持续集成服务器 ( Jenkins ),它会在每次提交代码时自动构建、部署和测试代码。

于 2013-08-17T09:03:11.553 回答