将JRE添加到Maven项目类路径

时间:2018-04-19 11:04:34

标签: java maven classpath

我是Maven的新手,我遇到了以下问题:我已经创建了一个示例Maven项目,并且我试图制造一些战争。我已经在我的pom.xml中添加了几个依赖项,但我无法弄清楚如何将JRE添加到classpath中。当我编译war模块时,我收到以下错误:

[ERROR] /C:/path/to/class/PreferredMapper.java:[3,44] package com.sun.xml.internal.bind.marshaller does not exist
[ERROR] /C:/path/to/class/PreferredMapper.java:[11,38] cannot find symbol
  symbol: class NamespacePrefixMapper

NamespaceprefixMapper包含在JRE的库rt.jar中,因此我猜测我的JRE不在编译类路径中。我已经尝试在我的pom.xml中添加JRE作为依赖项,但它无法正常工作。

将JRE库添加到类路径的正确方法是什么?

EDIT1:

战争模块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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.example</groupId>
    <artifactId>multi-module-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>war-module</artifactId>
<packaging>war</packaging>
<name>WarModule</name>
<description>WAR module</description>

<dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </dependency>
    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>com.ibm.ws.admin</groupId>
        <artifactId>client</artifactId>
    </dependency>
    <dependency>
        <groupId>com.ibm.ws.ejb</groupId>
        <artifactId>thinclient</artifactId>
    </dependency>
</dependencies>

这是一个带有ear and war模块的多模块项目。我试图通过运行mvn install命令将工件安装到我的本地存储库。

1 个答案:

答案 0 :(得分:0)

我会添加一个属性部分,其中包括以下内容(根据需要更改版本)

<properties>
    <java.version>1.8</java.version>
</properties>

然后添加编译器插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

我认为我已经看到了更优雅的做法,但这就是我以前所做的。