Maven project is missing package when compiling from terminal - java

I'm currently trying to compile my first maven project from the command line but whenever I run this command:
C:\Users\zacha\git\INF2050\tp1\PELZ07039904\src\main\java>javac analyse.java
I get this back
analyse.java:7: error: package org.apache.commons.io does not exist
But the project runs perfectly fine when I run it directly from InteliJ
Here is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>PELZ07039904</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
I checked and all my jar are in my .m2 file so I'm not sure why I'm getting this error.

javac doesn’t have any idea about maven and it’s dependencies. All it can do is just compile a class. Thats it.
Instead you need to use maven specific command.
Maven provides a command line tool.
To build a Maven project via the command line, run the mvn command from the command line. The command should be executed in the directory which contains the relevant pom file. You need to provide the mvn command with the life cycle phase or goal to execute.
The Maven tooling reads the pom file and resolves the dependencies of the project. Maven validates if required components are available in a local repository. The local repository is found in the .m2/repository folder of the users home directory.
For example the mvn clean install command triggers the jar packaging. This includes compiling the sources, executing the tests and packaging the compiled files in a JAR file. As last step the install phase installs the resulting artifact into the local repository, so it can be used as dependencies by other Maven builds.
Check full guide here

Related

How do I get maven to pick up my local jar in repositories?

I am writing a package (PACK-A) that is consumed by another (PACK-B). For local development I want to use a local jar that is produced from PACK-A and use it in PACK-B.
The workflow would be
cd PACK-A; mvn clean; mvn package
cd PACK-B; mvn clean; mvn package (but pulls in the local PACK-A jar file)
I thought the way to do it was through the <repositories /> tag in the pom.xml file so my POM.xml file looks like this (lots taken out for brevity):
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.team</groupId>
<artifactId>PACK-B</artifactId>
<version>1.0.0</version>
<dependencies>
<groupId>com.company.team</groupId>
<artifactId>service-name</artifactId>
<version>0.3.0-master+17</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>service-name</id>
<url>file:///Users/username/Projects/service-name/target</url>
</repository>
</repositories>
</project>
file:///Users/username/Projects/service-name/target is the directory that compiles (mvn package) the service.jar.
It isn't picking it up and I've confirmed it using a decompiler. How do I get maven to use my local jar in the service that I compiled? Or is my <url> directory written out incorrectly?
Also, I've confirmed, via the decompiler, that the service does compile with my changes. It just isn't picked up by my consumer.
For local development I want to use a local jar that is produced from PACK-A and use it in PACK-B.
To achieve this, you "only need":
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.team</groupId>
<artifactId>PACK-B</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.company.team</groupId>
<artifactId>PACK-A</artifactId>
<!-- same version, or which ever suits you -->
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
And to run mvn install (which additionally to package installs the artifact into the local repository).
See also: How to build dependent project when building a module in maven
and What does mvn install in maven exactly do
It is a "complex field" (multi-module...), but the thing You are trying to achieve is really simple/101.

Maven dependency lib path

I have created the sample maven project and it executes well as expected, however I would like to know the location of JUnit library.
I tried to find it in the local repository and my project path but I do not find the one.
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.maven.sample</groupId>
<artifactId>sampleproject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sampleproject</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Maven version: 3.3.9.
The file is certainly in your Maven cache, in the .m2/repository/junit/junit/3.8.1 folder, note the pattern: .m2/repository/<groupId>/<artifactId>/<version>
If you really want to have it automatically generated by Maven, use the Maven Dependency Plugin and its build-classpath goal.
From the command line on your project run:
mvn dependency:build-classpath
It will generate as part of the build output a list of paths for all the declared dependencies, so you will be sure where to look for.
Additionally, you can use the mdep.outputFile option to have it written to a file:
mvn dependency:build-classpath -Dmdep.outputFile=classpath.txt

Maven + Spring in Eclipse: Unable to find the downloaded Jars

I am new to using Maven and Eclipse, I am following this link.
Below is the pom.xml I have in my workspace:
<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.HelloWorld</groupId>
<artifactId>SpringHelloWorld</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringHelloWorld Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
<jdk.version>1.6</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>SpringHelloWorld</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
For adding the required jars, I run the below command from command prompt:
mvn eclipse:eclipse -Dwtpversion=2.0
I got the "BUILD SUCCESS" message in my console. But when I refresh the project in Eclipse IDE, I can see all the libraries are missing and the path for those jars does not exists.
e.g.
Description Resource Path Location Type
Project 'SpringHelloWorld' is missing required library: 'C:\Users\username\.m2\repository\org\springframework\spring-aop\3.0.5.RELEASE\spring-aop-3.0.5.RELEASE.jar' SpringHelloWorld Build path Build Path Problem
For this error, when I check manually, I can't see any folder named springframework inside the org folder, hence the jar is missing and the error is displaye din Eclipse IDE.
I tried running the command mvn eclipse:eclipse -Dwtpversion=2.0 multiple times but of no use.
Please advice how to resolve this.
EDIT
Below is the snapshot of the Eclipse Installations:
EDIT 2
I am not getting the Maven option when I Right Click on my project in Eclipse IDE. Hence when I create the java folder under the main folder, I am not able toUpdate Project Configuration` as mentioned here. Please advice
The Maven command mvn eclipse:eclipse generate the eclipse project files from POM.
The "-D" prefix in the argument means that it's a system property.
System property are defined like http://docs.oracle.com/javase/jndi/tutorial/beyond/env/source.html#SYS
mvn eclipse:eclipse -Dwtpversion=2.0 command convert the web based java project to maven based java project to support Eclipse IDE.
You can simple use given below command to clean and build your project:
mvn clean and mvn install
OR mvn clean install
Suggestion: Check maven is installed properly using command mvn --version.
Please try installing m2eclipse plugin (if you haven't done so yet) and nextly convert project to use maven behaviour through right clicking on project and choosing "convert to maven" option.
mvn eclipse:eclipse -Dwtpversion=2.0 -e clean install
run this instead. This builds your project by resolving dependencies

Maven doesn't put downloaded jars in eclipse's JRE System Library "folder"

I have created project using this command:
mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
After downloading libraries my project looks like this:
http://i43.tinypic.com/2iut1me.jpg
How can I "tell" maven to download jars in just one folder?
Maven will not download jars in a single folder, instead, it create a dir structure in a $HOME/.m2/repository for managing your dependencies, including versions.
If you want the jars to show in the Eclipse project, create a pom.xml file in your project with the correct dependencies, for example
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.myGroup</groupId>
<artifactId>myProject</artifactId>
<name>myProjectName</name>
<version>myProjectVersion</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>dependency1</groupId>
<artifactId>dependency1</artifactId>
<version>dependency1</version>
</dependency>
<dependency>
<groupId>dependency2</groupId>
<artifactId>dependency2</artifactId>
<version>dependency2</version>
</dependency>
<!-- .... and so on -->
</dependencies>
Also, configure your project in eclipse to be a Maven project and then you will see "Maven Dependencies"in your project.

How do I add the storm mongo library as a maven dependency?

I'm trying to use a SimpleMongoBolt from storm-contrib. I downloaded the source, entered the storm-contrib-mongo directory and ran mvn package and mvn install. Everything worked fine and IntelliJ was able to resolve things while coding. When I try to build my project, however, it tries to find a pom for this library on an external repository. When it can't find it, it fails. What do I need to do to fix this?
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>StormTest</groupId>
<artifactId>StormTest</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>StormTest</name>
<repositories>
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>storm</groupId>
<artifactId>storm</artifactId>
<version>0.7.2</version>
<scope>Test</scope>
</dependency>
<dependency>
<groupId>com.rapportive</groupId>
<artifactId>storm-amqp-spout</artifactId>
<version>0.1.1</version>
</dependency>
<dependency>
<groupId>com.rapportive</groupId>
<artifactId>storm-json</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>storm</groupId>
<artifactId>storm-contrib-mongo</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
You can try to check if you have proper dependencies defined in your project pom and compare them to storm artifact one. groupId, artifactId and version must be the same or Maven will try to download it from external repository and probably failed as it has never been installed on any public Maven repo.
When you install your artifact, it goes to user-directory/.m2/repostiory/group/id/path/*artifact/id/path*/version.
For your storm-amqp-spout you should have it in:
user-directory/.m2/repository/com/rapportive/storm-amqp-spout/0.1.1 folder.
There you should have few files:
jar itself (if it was packaged as jar file).
pom.xml file (the same you created for your project and you used to built and install it).
optionally sha1 files for both above.
If you don't have them, you probably made some mistake installing the artifact into repository. You can try to install it again or manually create pom just copying it from artifact source directory.
If there's correct pom.xml, I don't really have idea as I have never worked with IntelliJ (idea? ;)).
SimpleMongoBolt in Storm-Contrib was actually out of date. I updated the module myself and submitted a pull request, which has yet to be merged. Currently you can retrieve the updated code from my Storm-Contrib fork.

Categories