Maven 2 Maven:仅为打包jar指定outputDirectory?

Maven 2 Maven:仅为打包jar指定outputDirectory?,maven-2,maven,Maven 2,Maven,如何仅为打包jar指定outputDirectory 这显示了所有参数,但如何在命令行或pom.xml中设置它们?在命令行上 -DoutputDirectory=<path> -doutput目录= 在pom.xml中 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI

如何仅为打包jar指定outputDirectory

这显示了所有参数,但如何在命令行或pom.xml中设置它们?

在命令行上

-DoutputDirectory=<path>
-doutput目录=
在pom.xml中

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.3.1</version>
      <configuration>
        <outputDirectory>/my/path</outputDirectory>
      </configuration>
    </plugin>
  </plugins>
</build>

org.apache.maven.plugins
maven jar插件
2.3.1
/我的/路径
参数表达式 关于命令行用法:

指定将参数初始化为属性
${project.build.directory}
的值(该属性引用目标文件夹)

这意味着:

对于拟用于 直接从CLI执行,其 参数通常提供一种 可以通过系统属性进行配置 而不是
部分 在POM中。插件文档 对于这些参数,将列出 表示系统的表达式 配置的属性。在里面 在上面的mojo中,参数url是 与表达式关联
${query.url}
,表示其值可以是 由系统属性指定
query.url
如下所示:

参考资料:

正在配置${project.build.directory} 但是,
${project.build.directory}
不是的对象的系统属性

您不能直接在命令行上设置maven的内部属性,但可以通过在pom.xml中添加占位符来实现:

<build>
    <directory>${dir}</directory>
</build>
现在你可以

# everything goes in someOtherDir instead of target
mvn clean install -Pconf -Ddir=someOtherDir
# everything goes in someOtherDir instead of target
mvn clean install -Ddir=someOtherDir
还是老样子

# everything goes in target
mvn clean install
# everything goes in target
mvn clean install
配置Jar插件 现在,如果您只想从命令行更改jar outputDirectory而不从target重定向所有内容,我们将修改配置文件以从命令行属性配置插件:

<profiles>
    <profile>
        <id>conf</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.3.1</version>
                    <configuration>
                        <outputDirectory>${dir}</outputDirectory>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

感谢@Sean Patrick Floyd的出色解释

我不想创建一个概要文件并使用
mvn
始终按
-p
开关,而是想使用另一种方法,将属性${dir}设为默认值

只需将
${dir}
的默认值定义为
${project.build.directory}

<properties>
    <dir>${project.build.directory}</dir>
</properties>
还是老样子

# everything goes in target
mvn clean install
# everything goes in target
mvn clean install

如果您希望将依赖项JAR复制到目标文件夹,请使用maven依赖项插件

<project>
  ...
  ...

      <build>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>

</project>

...
...
maven依赖插件
安装
复制依赖项
${project.build.directory}/lib

我已经尝试过了:mvn-DoutputDirectory=/tmp包,但没有成功。我怎样才能告诉mvn这个选项应该由jar:jar目标使用呢?在命令行上,我认为您不能为signel pluginthanks指定它。这是我关于stackoverflow的第一个问题。响应时间给我留下了深刻的印象:-)您可以使用系统属性在命令行上指定一些参数,对于其他参数,您必须使用技巧(请参见我的答案)来构建项目顶级目录,请使用:${project.basedir}
# everything goes in target
mvn clean install
<project>
  ...
  ...

      <build>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>

</project>