【发布时间】:2013-01-31 21:44:33
【问题描述】:
我浏览了 StackOverflow 上的所有内容,但找不到答案。我刚刚构建了一台新机器,并且我的项目在以前的安装上编译和构建都很好。但是我的新机器不会。一切似乎都在正确构建(没有构建错误,正确包含依赖项),但我的项目中的实际类文件(我编写的代码)不包含在 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 3.0.4。
-
深入研究我的 maven 安装后,我发现了多个实例(3.0.3 和 2.2.1)。我需要清理它并重试...谢谢。
-
不走运。我完全卸载并重新安装了 Maven 并升级到最新版本(3.0.4),我仍然得到一个没有类文件的 jar,现在清单也没有列出主类。 Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: johnblakie Build-Jdk: 1.6.0_37
-
附加说明。我使用的是 MacOS 10.7.5,Java 版本 1.6.0_37。
-
我在 Windows 上做了一个干净的设置,得到了相同的结果。没有类文件。直接 Maven 3.0.4 和 Java 1.6.0_39。你确定你有实际的类文件,而不仅仅是一个参考。 jar 中唯一的目录输出是 META-INF 目录中的一个属性和一个 pom.xml 文件。如我所料,没有 com/zoverge/utils/HelloWorld.class 的目录。
标签: java maven build-process executable-jar