java - Maven Build 不在 jar 中包含 .class 文件

标签 java maven build-process executable-jar

我已经查看了在 StackOverflow 上可以找到的所有内容,但找不到答案。我刚刚 build 了一台新机器,我的项目在以前的安装上编译和构建得很好。但是我的新机器不会。一切似乎都在正确构建(没有构建错误,正确包含依赖项),但我项目中的实际类文件(我编写的代码)未包含在 jar 中。然后我创建了一个简单的 helloWorld 项目,其中包含一些最小的 POM 文件,以查看问题是否仍然存在。我从命令行进行了构建,以便将 Eclipse 排除在外。

类代码

package com.zoverge.utils;

public class HelloWorld {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String message = args[0];
        System.out.println(message);
    }

}

我的 POM 文件

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zoverge.utils</groupId>
  <artifactId>helloworld</artifactId>
  <packaging>jar</packaging>
  <version>1.0</version>
  <name>Hello World</name>
  <url>http://maven.apache.org</url>
  <properties>
            <project.build.java.version>1.6</project.build.java.version>
            <org.apache.maven.plugins.maven-compiler-plugin.version>2.5.1</org.apache.maven.plugins.maven-compiler-plugin.version>          
  </properties>
  <build>
        <plugins>
            <!-- Maven will compile our source java classes using the "project.build.java.version" 
                specified -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${org.apache.maven.plugins.maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${project.build.java.version}</source>
                    <target>${project.build.java.version}</target>
                </configuration>
            </plugin>
  <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.zoverge.utils.HelloWorld</mainClass>
            </manifest>
          </archive>
                <finalName>HelloWorld</finalName>
                <appendAssemblyId>false</appendAssemblyId>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
  </plugins>
  </build>
<dependencies/>
</project>

生成的构建生成 2 个 jar 文件(如预期)helloworld-1.0.jar 和 HelloWorld.jar。这两个 jar 都不包含 HelloWorld.java 的已编译类文件。我认为这一定是我的环境有问题,因为这种行为对于在其他开发机器上运行良好的其他项目是相同的,但我似乎无法弄清楚其中的区别。我无权访问我以前的开发机器来比较配置。我的 .m2/setting.xml 基本上是空的。

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository>${user.home}/.m2/repository</localRepository>
  <interactiveMode/>
  <usePluginRegistry/>
  <offline/>
  <pluginGroups/>
  <servers/>
  <mirrors/>
  <proxies/>
  <profiles/>
  <activeProfiles/>
</settings>

最佳答案

我发现了我的问题。问题是文件夹结构。 Maven 采用/src/main/java/结构。我的结构不同,并且没有在 POM.xml 中被覆盖。匹配标准路径或覆盖 POM.xml 中的路径解决了问题。

关于java - Maven Build 不在 jar 中包含 .class 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14902797/

相关文章:

java - Maven 配置问题一个或多个约束尚未满足

java - 在 `WEB-INF/lib` 文件夹和 `src/main/resources` 文件夹中找不到 jar 文件的解决方案?

c++ - 如何根据目标文件中缺少的符号来实例化模板?

java - 可以用java连接MySQL数据库,但不能用PHP连接?

java - 数学二十四游戏 Java

java - JSF 应用程序为子上下文提供 404

cocoa - 您可以使用 CruiseControl 构建 Cocoa/Objective-C 项目吗?

java - 为什么 Collections.binarySearch 给出错误的结果?

maven - 通过 Spring Boot 在 jar 文件中提供静态资源

c++ - Eclipse C++ 构建问题