有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从命令行创建Java包的正确方法是什么?(与Maven的麻烦)

[已解决]-但仍在寻找解释。请参见问题底部


我对命令行或Maven没有太多经验。我正在写一本书中的教程。它说明我应该创建一个java文件

src/main/java/com/effectivemaven/chapter01/ExampleAction.java

我所做的是分别对每个目录进行{}mkdir commkdir effectivemavenmkdir 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 the target?


编辑树

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 package com\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) 个答案

  1. # 1 楼答案

    好的,groupid与应用程序结构无关,它是应用程序名称的一部分。您正在构建一个webapp,因此编译后的类进入生成的war文件中的WEB-INF/classes目录(根据您的pom,该文件称为first-webapp.war),可以在target目录中找到。您需要目录src/main/javasrc/main/resourcessrc/test/javasrc/test/resourcessrc/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目录中