我刚刚安装了Maven 3 - 我是Java的新手,我想了解Maven的工作原理。现在,我知道我可以通过在maven项目中编辑POM文件来添加依赖项,但我想知道是否有一种“干净”的方式来使用命令行。 运行Windows 8.1,Java 8,Maven 3。
答案 0 :(得分:4)
直接使用POM可能是一种更好的方法,但是你可以这样做:
mvn install:install-file -Dfile=<path-to-file> -DgroupId="group-id" \
-DartifactId="artifact-id" -Dversion="version" -Dpackaging="packaging"
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
答案 1 :(得分:0)
我不太确定是否有内置的Maven功能或插件,但是我认为不是。您可以使用sed
或类似的脚本通过一个小的脚本来实现。我在bash脚本中使用了以下内容:
get_foo_dep() {
cat <<EOF
<dependency>
<groupId>com.example</groupId>
<artifactId>foo</artifactId>
<version>${DEP_VERSION}</version>
</dependency>
EOF
}
DEP_VERSION="1.0.0" # just example, you can set it from somewhere
POM_FILE="pom.xml" # just example, the path to pom
foo_dep=$(get_foo_dep)
foo_dep=${foo_dep//$'\n'/\\$'\n'} # escape \n with \\n for sed to work
sed -i "s|<dependencies>|<dependencies>\n${foo_dep}|" "$POM_FILE" # CARE!! it makes in-place substitution
您也可以看看另一个问题,建议使用awk
:
Is it possible to use sed to replace dependencies's version in a pom file?