Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to create a Java package from command line? (trouble with Maven)

[ SOLVED ] - but still looking for explanation. Please see bottom of question


I don't have much experience with command line or with Maven. I'm working on a tutorial from a book. It state I should create a java file

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

What I did was mkdir each directory separately i.e. mkdir com, mkdir effectivemaven, mkdir chapter01

I create the .java file in the chapter01 directory.

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;
    }
}

When I mvn compile, it says compiling 1 file to target..., but when I look at the taget directory, nothing is created.

So I tried to create a another .java file without using packages, just a simple

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

in the java directory, then mvn compile and it shows up in the target file.

So my assumption is that I'm not creating the packages correctly by using mkdir, or possibly I'm doing something else wrong I'm unaware of.

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?


EDIT tree

first-webapp
          src
             main
                 java
                     Hello.java
                     com
                        effectivemaven
                                     chapter01
                                             ExampleAction.java
          target
               classes
                     Hello.class
          pom.xml

Command running from 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>

EDIT pom.xml as requested

<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>

By the way, this is a web-app I created with the following command

mvn archetype-generate -DgroupId=com.underdogdevs.webapp -DartifactId=first-webapp -DarchetypeArtifactid=maven-archetype-webapp


[ SOLVED ] - but with very mimimal understanding - still offering brownie points for anyone who can explain to me this behavior. Below is what I did

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?

like image 930
Paul Samsotha Avatar asked Nov 11 '22 15:11

Paul Samsotha


1 Answers

Ok, the groupid isn't related to your application structure -- it's part of your application name. You are building a webapp, so the compiled classes go into the WEB-INF/classes directory in the generated war file (which, according to your pom is called first-webapp.war) which can be found in the target directory. You need directories src/main/java, src/main/resources, src/test/java, src/test/resources, src/main/webapp/WEB-INF. Under the java directory you should put your package structure (more directories), which in this case is com/effectivemaven/chapter01 and under that put your java file ExampleAction.java. Put your pom in your project root.

On the command line run mvn clean install and you'll generate the target and you should find your .war file in there. In that, under the WEB-INF/classes/com/effectivemavem/chapter01 directory you'll find your compiled java class with a .class extension.

At some point you'll have to put a web.xml file in the WEB-INF directory.

like image 143
Software Engineer Avatar answered Nov 15 '22 06:11

Software Engineer