只有在Eclipse中关闭项目时,Maven对工作空间中项目的依赖才有效

时间:2014-03-09 11:33:30

标签: java eclipse maven jar m2e

我正在使用带有M2E插件的Eclipse Kepler for Maven。

我想使用我的“Util”Maven项目创建web-app Maven项目 - 这是不是多模块项目。只需要在项目中使用这个简单的.jar,就可以在主项目工作期间编辑“Util”项目,增强和修复。

我在webb应用程序中只添加了maven依赖项(没有像构建路径,部署程序集等那样的设置),Eclipse自动计算出这是来自工作区的项目(简单地说:不看作带有版本号的.jar但带有文件夹图标)

现在,当我在maven repo和关闭项目install“Util”时,一切正常,Eclipse正在将我的util-0.0.1-SNAPSHOT.jar部署到web-inf / lib工作.jar文件。

问题是:当util项目打开时,eclipse只在部署中创建“util.jar”(不是util-0.0.1-SNAPSHOT.jar)并将其部署为“jar-like” -war“将类放入WEB-INF / classes /而不是jar的根目录,所以我最终会得到类似的东西:

...\wtpwebapps\BigProject\WEB-INF\lib\util.jar\WEB-INF\classes\结果是简单的ClassNotFoundException,因为“Util”项目不是一个网络项目 - 只有简单类的.jar。

如何在Web应用程序中为简单的util项目添加Maven依赖项,还是可以选择在工作区中随时编辑util项目?

1 个答案:

答案 0 :(得分:0)

拥有与您相同的设置,并且工作正常。当我处理小项目并且mvn install JAR没有作为projName-0.0.1-SNAPSHOT.jar部署在目标时。两个项目都有相同的<groupID>吗?

修改 有一些时间,也准备好了所以我做了一些测试。对于长篇文章我很抱歉,但这对我有用。我正在使用WildFly服务器。

1 / <groupID>并非必须在所有

之后相同

2a /这是pom.xml jar

utils
<?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/maven-v4_0_0.xsd">

  <!-- =========================================================== -->
  <!-- Basics -->
  <!-- =========================================================== -->

    <modelVersion>4.0.0</modelVersion>
    <groupId>so.examples</groupId>
    <artifactId>utils</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <name>WebApp Common files</name>
    <description>WebApp Common files</description>

  <!-- =========================================================== -->
  <!-- Properties -->
  <!-- =========================================================== -->

    <properties>

        <!-- other plugin versions -->
        <version.compiler.plugin>2.3.2</version.compiler.plugin>
        <version.surefire.plugin>2.4.3</version.surefire.plugin>

        <!-- maven-compiler-plugin -->
        <maven.compiler.target>1.7</maven.compiler.target>

    </properties>


    <dependencyManagement>
        <dependencies>
         </dependencies>
    </dependencyManagement>


    <dependencies>

    </dependencies>

    <build>
        <!-- Maven will append the version to the finalName -->
        <finalName>${project.artifactId}</finalName>


        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${version.compiler.plugin}</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>

        </plugins>

    </build>
</project>

2b /和类文件

package utils.commons;


public class PrintUtilities 
{   

    public static String addArrowPrint(String toPrint)
    {
        return "--->"+toPrint+"<----";      
    }
}

3a /以下是pom.xml

webApp
<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>so.examples</groupId>
<artifactId>webapp</artifactId>
<packaging>war</packaging>
<version>0.0.1</version>
<description>Main WebApp project</description>


<properties>
    <!-- Explicitly declaring the source encoding eliminates the following
        message: -->
    <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered
        resources, i.e. build is platform dependent! -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- JBoss dependency versions -->
    <version.jboss.maven.plugin>7.4.Final</version.jboss.maven.plugin>

    <!-- Define the version of the JBoss BOMs we want to import to specify
        tested stacks. -->
    <version.jboss.bom>1.0.7.Final</version.jboss.bom>

    <!-- other plugin versions -->
    <version.surefire.plugin>2.10</version.surefire.plugin>
    <version.war.plugin>2.1.1</version.war.plugin>

    <!-- maven-compiler-plugin -->
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>
</properties>


<dependencyManagement>
    <dependencies>
        <!-- JBoss distributes a complete set of Java EE 6 APIs including a Bill
            of Materials (BOM). A BOM specifies the versions of a "stack" (or a collection)
            of artifacts. We use this here so that we always get the correct versions
            of artifacts. Here we use the jboss-javaee-6.0-with-tools stack (you can
            read this as the JBoss stack of the Java EE 6 APIs, with some extras tools
            for your project, such as Arquillian for testing) and the jboss-javaee-6.0-with-hibernate
            stack you can read this as the JBoss stack of the Java EE 6 APIs, with extras
            from the Hibernate family of projects) -->
        <dependency>
            <groupId>org.jboss.bom</groupId>
            <artifactId>jboss-javaee-6.0-with-tools</artifactId>
            <version>${version.jboss.bom}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.bom</groupId>
            <artifactId>jboss-javaee-6.0-with-hibernate</artifactId>
            <version>${version.jboss.bom}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>so.examples</groupId>
        <artifactId>jar_lib</artifactId>
        <version>0.0.1</version>
    </dependency>

    <!-- First declare the APIs we depend on and need for compilation. All
        of them are provided by JBoss AS 7 -->

    <!-- Import the Common Annotations API (JSR-250), we use provided scope
        as the API is included in JBoss AS 7 -->
    <dependency>
        <groupId>org.jboss.spec.javax.annotation</groupId>
        <artifactId>jboss-annotations-api_1.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Import the JAX-RS API, we use provided scope as the API is included
        in JBoss AS 7 -->
    <dependency>
        <groupId>org.jboss.spec.javax.ws.rs</groupId>
        <artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Import the EJB API, we use provided scope as the API is included in
        JBoss AS 7 -->
    <dependency>
        <groupId>org.jboss.spec.javax.ejb</groupId>
        <artifactId>jboss-ejb-api_3.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>


    <!-- Import the JSF API, we use provided scope as the API is included in
        JBoss AS 7 -->
    <dependency>
        <groupId>org.jboss.spec.javax.faces</groupId>
        <artifactId>jboss-jsf-api_2.1_spec</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Now we declare any tools needed -->

</dependencies>

<build>
    <!-- Maven will append the version to the finalName (which is the name
        given to the generated war, and hence the context root) -->
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>${version.war.plugin}</version>
            <configuration>
                <!-- Java EE 6 doesn't require web.xml, Maven needs to catch up! -->
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <!-- The JBoss AS plugin deploys your war to a local JBoss AS container -->
        <!-- To use, run: mvn package jboss-as:deploy -->
        <plugin>
            <groupId>org.jboss.as.plugins</groupId>
            <artifactId>jboss-as-maven-plugin</artifactId>
            <version>${version.jboss.maven.plugin}</version>
        </plugin>
        <plugin>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <version>1.0.1.Final</version>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <!-- The default profile skips all tests, though you can tune it to run
            just unit tests based on a custom pattern -->
        <!-- Seperate profiles are provided for running all tests, including Arquillian
            tests that execute in the specified container -->
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${version.surefire.plugin}</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>


</profiles> 

3b /以下是类文件:

RESTActivator

package webapp;


import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/root")
public class RESTActivator extends Application {


}

RestMethods

包webapp;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.*;

import utils.commons.PrintUtilities;

@Stateless
@LocalBean
@Path("/methods")
public class RestMethods  {


    @GET()
    @Produces("text/plain")
    public String welcomeMessage()
    {
        StringBuffer welcomeText = new StringBuffer();

        welcomeText.append(" Called  Rest Methods  \n");
        welcomeText.append(" ====================  \n");
        welcomeText.append(PrintUtilities.addArrowPrint("Called  Rest Methods") + "\n");        

        return welcomeText.toString();
    }
}//class

4 /我首先为mvn clean install项目执行utils。安装在我当地的maven仓库后。然后在我的Web应用程序项目上使用eclipse执行update project,然后clean package wildfly:deploy(服务器必须已经启动)。

然后将浏览器指向http://localhost:8080/webapp/root/methods

希望这有帮助。