从命令行创建Java包的正确方法是什么?(与Maven的麻烦)
[已解决]-但仍在寻找解释。请参见问题底部
我对命令行或Maven没有太多经验。我正在写一本书中的教程。它说明我应该创建一个java文件
src/main/java/com/effectivemaven/chapter01/ExampleAction.java
我所做的是分别对每个目录进行{mkdir com
、mkdir effectivemaven
、mkdir chapter01
我创建了一个。chapter01
目录中的java文件
package com.effectivemaven.chapter01;
import org.slf4j.*;
public class ExampleAction {
final Logger logger = LoggerFactory.getLogger(ExampleAction.class);
public boolean execute() {
logger.info( "Example Action Executed." );
return true;
}
}
当我mvn compile
时,它说compiling 1 file to target...
,但是当我查看taget目录时,没有创建任何内容
所以我试着创造另一个。java文件没有使用包,只是一个简单的
public class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
在java
目录中,然后mvn compile
,它显示在target
文件中
所以我的假设是,我没有通过使用mkdir
正确地创建包,或者可能是我做了一些我不知道的其他错误
So basically I just want to know, what is the correct way to create a package from the command line? And if I'm doing it correctly, what could be the other possible reasons the
.class
is not being created in thetarget
?
编辑树
first-webapp
src
main
java
Hello.java
com
effectivemaven
chapter01
ExampleAction.java
target
classes
Hello.class
pom.xml
从C:\Maven Book\first-webapp>
运行的命令
C:\Maven Book\first-webapp>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building first-webapp Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ first-weba
pp ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ first-webapp
---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 2 source files to C:\Maven Book\first-webapp\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.667s
[INFO] Finished at: Sun Jan 12 00:09:49 PST 2014
[INFO] Final Memory: 11M/111M
[INFO] ------------------------------------------------------------------------
C:\Maven Book\first-webapp>
编辑pom。根据请求使用xml
<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.underdogdevs.webapp</groupId>
<artifactId>first-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>first-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
<build>
<finalName>first-webapp</finalName>
</build>
</project>
顺便说一下,这是我用以下命令创建的一个web应用程序
mvn archetype-generate -DgroupId=com.underdogdevs.webapp -DartifactId=first-webapp -DarchetypeArtifactid=maven-archetype-webapp
[SOLVED]-但理解力很低-仍然为任何能向我解释这种行为的人提供布朗尼点数。下面是我所做的
I got it to work. Since I create the project with
groupId=com.underdogdevs.webapp
, I tried to make a packagecom\underdogdevs\webapp
and created a the class with the corresponding package reference. This fixed the problem. The class appears. But I tested it even further and deleted the package I just created and tried to clean and compile with only the original package structure, but the file showed up again in the orginal package structure. I have no idea why this happens though.Anyone have any ideas?
# 1 楼答案
好的,
groupid
与应用程序结构无关,它是应用程序名称的一部分。您正在构建一个webapp,因此编译后的类进入生成的war文件中的WEB-INF/classes
目录(根据您的pom,该文件称为first-webapp.war
),可以在target
目录中找到。您需要目录src/main/java
、src/main/resources
、src/test/java
、src/test/resources
、src/main/webapp/WEB-INF
。在java目录下,您应该放置包结构(更多目录),在本例中是com/effectivemaven/chapter01
,在该目录下放置java文件ExampleAction.java
。将pom
放在项目根目录中在命令行上运行
mvn clean install
,您将生成目标,您应该在其中找到您的.war
文件。这样,在WEB-INF/classes/com/effectivemavem/chapter01
目录下,您将发现编译后的java类具有.class
扩展名在某个时候,您必须将
web.xml
文件放在WEB-INF
目录中