Set Up The Classpath中文官方文档

原文地址

这里写目录标题

  • 设置 Classpath
    • Add A Class-Path Entry To The Manifest
    • 使 Jar 可执行
    • 修改 Classpath: 定义 Classpath 目录前缀
    • 修改 Classpath: 使用 Maven Repository-Style Classpath

设置 Classpath

Add A Class-Path Entry To The Manifest

Maven Archiver 可以将 classpath 添加到你的项目中.这依赖于 配置属性.

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-jar-pluginartifactId>
            ...
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>trueaddClasspath>
                manifest>
              archive>
            configuration>
            ...
          plugin>
        plugins>
      build>
      ...
      <dependencies>
        <dependency>
          <groupId>commons-langgroupId>
          <artifactId>commons-langartifactId>
          <version>2.1version>
        dependency>
        <dependency>
          <groupId>org.codehaus.plexusgroupId>
          <artifactId>plexus-utilsartifactId>
          <version>1.1version>
        dependency>
      dependencies>
      ...
    project>

清单文件将产生如下的配置:

    Manifest-Version: 1.0
    Created-By: Apache Maven ${maven.version}
    Build-Jdk: ${java.version}
    Class-Path: plexus-utils-1.1.jar commons-lang-2.1.jar

使 Jar 可执行

如果您希望创建一个可执行jar文件,则需要相应地配置Maven Archiver。您需要告诉它使用哪个主类。这是通过配置元素完成的。下面是一个示例pom.xml,它配置为添加类路径并使用fully.qualified.MainClass作为主类:

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-jar-pluginartifactId>
            ...
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>trueaddClasspath>
                  <mainClass>fully.qualified.MainClassmainClass>
                manifest>
              archive>
            configuration>
            ...
          plugin>
        plugins>
      build>
      ...
      <dependencies>
        <dependency>
          <groupId>commons-langgroupId>
          <artifactId>commons-langartifactId>
          <version>2.1version>
        dependency>
        <dependency>
          <groupId>org.codehaus.plexusgroupId>
          <artifactId>plexus-utilsartifactId>
          <version>1.1version>
        dependency>
      dependencies>
      ...
    project>

清单文件将产生如下的配置:

    Manifest-Version: 1.0
    Created-By: Apache Maven ${maven.version}
    Build-Jdk: ${java.version}
    Main-Class: fully.qualified.MainClass
    Class-Path: plexus-utils-1.1.jar commons-lang-2.1.jar

修改 Classpath: 定义 Classpath 目录前缀

有时,能够修改类路径是有用的,例如在创建瘦war文件时。这可以通过配置 元素来实现。

project>
  ...
  <build>
    <plugins>
      <plugin>
         <artifactId>maven-war-pluginartifactId>
         <configuration>
           <archive>
             <manifest>
               <addClasspath>trueaddClasspath>
               <classpathPrefix>lib/classpathPrefix>
             manifest>
           archive>
         configuration>
      plugin>
    plugins>
  build>
  ...
  <dependencies>
    <dependency>
      <groupId>commons-langgroupId>
      <artifactId>commons-langartifactId>
      <version>2.1version>
    dependency>
    <dependency>
      <groupId>org.codehaus.plexusgroupId>
      <artifactId>plexus-utilsartifactId>
      <version>1.1version>
    dependency>
  dependencies>
  ...
project>

清单文件将产生如下的配置:

Class-Path: lib/plexus-utils-1.1.jar lib/commons-lang-2.1.jar

修改 Classpath: 使用 Maven Repository-Style Classpath

(自从: 2.3, 请看下面)
有时候,您可能希望在归档文件中包含Maven储存库样式的目录结构。如果你想在你的清单类路径中引用这些目录中的依赖档案,尝试使用 值为’repository’的元素,像这样:

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-jar-pluginartifactId>
            <version>2.3version>
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>trueaddClasspath>
                  <classpathPrefix>lib/classpathPrefix>
                  <classpathLayoutType>repositoryclasspathLayoutType>
                manifest>
              archive>
            configuration>
          plugin>
        plugins>
      build>
      ...
      <dependencies>
        <dependency>
          <groupId>commons-langgroupId>
          <artifactId>commons-langartifactId>
          <version>2.1version>
        dependency>
        <dependency>
          <groupId>org.codehaus.plexusgroupId>
          <artifactId>plexus-utilsartifactId>
          <version>1.1version>
        dependency>
      dependencies>
      ...
    project>

你可能感兴趣的:(java,maven)