从 CLI 部署 Maven 项目?

Posted

技术标签:

【中文标题】从 CLI 部署 Maven 项目?【英文标题】:deploy maven project from the CLI? 【发布时间】:2018-04-02 14:57:49 【问题描述】:

在 IDE 中构建和运行良好:

cd /home/thufir/NetBeansProjects/HelloMaven; JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 /home/thufir/.local/share/umake/ide/netbeans/java/maven/bin/mvn "-Dexec.args=-classpath %classpath net.bounceme.dur.hello_maven.Main" -Dexec.executable=/usr/lib/jvm/java-8-openjdk-amd64/bin/java process-classes org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
Scanning for projects...

------------------------------------------------------------------------
Building HelloMaven 1.0-SNAPSHOT
------------------------------------------------------------------------

--- maven-resources-plugin:2.5:resources (default-resources) @ HelloMaven ---
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
Copying 0 resource

--- maven-compiler-plugin:2.3.2:compile (default-compile) @ HelloMaven ---
Compiling 1 source file to /home/thufir/NetBeansProjects/HelloMaven/target/classes

--- exec-maven-plugin:1.2.1:exec (default-cli) @ HelloMaven ---
Oct 21, 2017 10:21:38 AM net.bounceme.dur.hello_maven.Main getGreeting
INFO: Hello world.
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.994s
Finished at: Sat Oct 21 10:21:38 PDT 2017
Final Memory: 10M/153M
------------------------------------------------------------------------

但在“CLI”中失败:

thufir@dur:~/NetBeansProjects/HelloMaven$ 
thufir@dur:~/NetBeansProjects/HelloMaven$ mvn clean deploy -e
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building HelloMaven 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ HelloMaven ---
[INFO] Deleting /home/thufir/NetBeansProjects/HelloMaven/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloMaven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloMaven ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/thufir/NetBeansProjects/HelloMaven/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloMaven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/thufir/NetBeansProjects/HelloMaven/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HelloMaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloMaven ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloMaven ---
[INFO] Building jar: /home/thufir/NetBeansProjects/HelloMaven/target/HelloMaven-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ HelloMaven ---
[INFO] Installing /home/thufir/NetBeansProjects/HelloMaven/target/HelloMaven-1.0-SNAPSHOT.jar to /home/thufir/.m2/repository/com/mycompany/HelloMaven/1.0-SNAPSHOT/HelloMaven-1.0-SNAPSHOT.jar
[INFO] Installing /home/thufir/NetBeansProjects/HelloMaven/pom.xml to /home/thufir/.m2/repository/com/mycompany/HelloMaven/1.0-SNAPSHOT/HelloMaven-1.0-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ HelloMaven ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.353 s
[INFO] Finished at: 2017-10-21T10:22:23-07:00
[INFO] Final Memory: 14M/49M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project HelloMaven: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project HelloMaven: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:309)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:194)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:107)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:993)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:345)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:191)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
    at org.apache.maven.plugin.deploy.DeployMojo.getDeploymentRepository(DeployMojo.java:235)
    at org.apache.maven.plugin.deploy.DeployMojo.execute(DeployMojo.java:118)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 20 more
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
thufir@dur:~/NetBeansProjects/HelloMaven$ 

最简单的项目:

thufir@dur:~/NetBeansProjects/HelloMaven$ 
thufir@dur:~/NetBeansProjects/HelloMaven$ tree
.
├── nbactions.xml
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── net
    │   │       └── bounceme
    │   │           └── dur
    │   │               └── hello_maven
    │   │                   └── Main.java
    │   └── resources
    └── test
        └── java

10 directories, 3 files
thufir@dur:~/NetBeansProjects/HelloMaven$ 

这是pom.xml

thufir@dur:~/NetBeansProjects/HelloMaven$ 
thufir@dur:~/NetBeansProjects/HelloMaven$ cat pom.xml 
<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>HelloMaven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>thufir@dur:~/NetBeansProjects/HelloMaven$ 

如何从 CLI 构建和运行此项目?

类似于:

Maven: lifecycle phase to run a program?

【问题讨论】:

部署目标失败,您的 IDE 没有尝试运行... 您将尝试部署到的存储库添加到 pom。 根本不尝试添加或使用存储库。但是,是的,查看distributionManagement 标签。 @f1sh 啊,我明白了。那么什么目标会起作用?奇怪的是 NB 没有部署。 ***.com/a/46866373/262852 运行。 【参考方案1】:

您的错误消息说 exec-maven-plugin 找不到主类。您的 pom 似乎没有将其列为插件。 对于完整的包名,不要包括 src.main.java,只需以域开头。通常是 com,org 等。

【讨论】:

以上是关于从 CLI 部署 Maven 项目?的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Eclipse 将 Maven 项目的 WAR 部署到 JBoss 服务器?

使用 maven 部署 javafx hsqldb 项目

Maven 无法从父 pom 部署

部署maven及Nexus私服

Angular4 + MVC + cli + 使用从 cli build 命令生成的 dist 文件夹进行部署

WSO2和创建可部署的归档文件从Maven获取旧属性