Locally installed jar not seen by maven - java

I have source code for a framework, let's call it my-framework. It provides various packages, including com.not_telling.framework.db.
I have a pom.xml file for this framework:
<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.not_telling</groupId>
<artifactId>my-famework</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>my-framework Maven Library</name>
<url>http://maven.apache.org</url>
<!-- more things here... -->
</project>
I can compile this and install it to my local repository cache, using this command:
mvn install
End of the install log:
[INFO] Installing C:\Users\MyUserName\my-framework\backend\target\my-framework.jar to C:\Users\MyUserName\.m2\repository\com\not_telling\my-famework\0.0.1-SNAPSHOT\my-famework-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\MyUserName\my-framework\backend\pom.xml to C:\Users\MyUserName\.m2\repository\com\not_telling\my-famework\0.0.1-SNAPSHOT\my-famework-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Then I have another project, that tries to use this as a dependency:
<dependency>
<groupId>com.not_telling</groupId>
<artifactId>my-famework</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
The compilation phase of this project fails with this message:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project my-project: Compilation failure: Compilation failure:
[ERROR] /C:/Users/MyUserName/workspace/my-project/backend/src/main/java/com/not_telling/some_example.java:[13,17] package com.not_telling.framework.db does not exist
Why is that? Maybe because the jar is in the repository cache, but not in a local repository?
This question is a follow up of absolute maven pomderived entry added to classpath - I realized that instead of doing magic with relative source directory references, I need to make a separate module, but I'm facing this problem.

It was bleeding from numerous wounds. These needed to be fixed:
move source files from under src/ to under src/main/java, in both the framework and the application project
change the group and the artifact id to match the package name E.g. for a package that is named "com.not_telling.framework" I had to use groupId=com.not_telling and name=framework.
The dependency scope was wrong in the application's pom file. I had to remove test
Now it works from command line I can do "mvn install" for the framework, and then "mvn package" for the applications.
The only problem left is that the application cannot be compiled from eclipse, I had to remove all linked source folders / build paths in the application project to the framework.

Related

Error When Building App with Maven Using Heroku

I'm trying to deploy my Discord bot using Heroku but everytime I deploy it, it gives me an error. The code works on local using IntelliJ Idea. Can anyone help me correct this error please?
Build log:
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 21 source files to /tmp/build_8fdb6208/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.237 s
[INFO] Finished at: 2021-03-21T23:52:06Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project CalciteDiscordBot: Fatal error compiling: invalid target release: 15 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
! ERROR: Failed to build app with Maven
We're sorry this build is failing! If you can't find the issue in application code,
please submit a ticket so we can help: https://help.heroku.com/
! Push rejected, failed to compile Java app.
! Push failed
Here's my pom.xml:
<?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>CalciteDiscordBot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>15</maven.compiler.target>
<maven.compiler.source>15</maven.compiler.source>
</properties>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.2.0_168</version>
</dependency>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</project>
Someone just helped me with this issue yesterday your Maven POM specifies that you want Java 15, but the Java version installed cannot handle compilation for 15. So, add a system.properties file to your application root with the following contents and build again:
java.runtime.version=15
You application requires Java 15 (<maven.compiler.source>15</maven.compiler.source>) so you need tell Heroku you want that specific Java version (instead of the default one)
In the root folder place a file system.properties:
java.runtime.version=15

Dependency error in eclipse project making use of Maven

I am using Eclipse Mars to run my Java project. I am making use of Maven in it. But while trying to compile my package I am getting the following error.
Failed to execute goal on project apex-check: Could not resolve dependencies for project org.fundacionjala.enforce.sonarqube:apex-check:jar:1.0: Failed to collect dependencies at org.fundacionjala.enforce.sonarqube:apex-squid:jar:1.0: Failed to read artifact descriptor for org.fundacionjala.enforce.sonarqube:apex-squid:jar:1.0: Could not transfer artifact org.fundacionjala.enforce.sonarqube:apex-squid:pom:1.0 from/to central (https://repo.maven.apache.org/maven2): Failed to authenticate with proxy -> [Help 1].
I am able to find that my pom.xml has a bug in its dependency. But don't know how to resolve it. I have given below my pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- ~ Copyright (c) Fundacion Jala. All rights reserved. ~ Licensed under the MIT license. See LICENSE file in the project root for full license information. -->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.fundacionjala.enforce.sonarqube</groupId>
<artifactId>apex</artifactId>
<version>1.0b${build.number}</version>
</parent>
<artifactId>apex-check</artifactId>
<name>Apex :: Checks</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apex-squid</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
I have written 'apex-check' and 'apex-squid' as two separate projects.
Can anyone explain how to correct my pom.xml?
You need to have:
org.fundacionjala.enforce.sonarqube:apex-check:jar:1.0
org.fundacionjala.enforce.sonarqube:apex-squid:jar:1.0
jar files available in your local .m2/repository folder. If they are not present maven will try to download from central repository and as an expected result it will fail on firewall or it will not find the artifact. i.e. if:
apex-check requires apex-squid project as dependency first you need to install squid project files by using mvn install on squid project folder.
But it seems more like you want to create a multi module maven project, take a look at this question. Create a parent project similar to this project, add check and squid as module and run mvn clean install on parent.
**edit: I just see you already have parent, so make sure parent has your projects as modules, which helps reactive build order and eclipse imports

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

Can't create maven project from console on Windows - error

I tried to generate maven project in Windows XP.
I created folder with pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>selenium.web.driver</groupId>
<artifactId>OMS_selenium_Test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>selenium_tests</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope></scope>
</dependency>
</dependencies>
</project>
Opened cmd and going into this folder.
Run the next command:
C:\Documents and Settings\Admintemp\maven_test>mvn archetype:generate -DgroupId=
selenium.web.driver -DartifactId=OMS_selenium_Test -Dversion=1.0-SNAPSHOT -Dinte
ractiveMode=false
Instead of generating project with this data I caught next errror:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2
.2:generate (default-cli) on project OMS_selenium_Test: Unable to add module to
the current project as it is not of packaging type 'pom' -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureExc
eption
Why this exactly happen?
Maybe I miss smt, but I can't figure out what exactly...
How to solve this trouble and generate project with maven?
Your mistake has been to create a POM file by hand. Maven will do this automatically when you run the command you attempted. Delete your POM file and try running the command again.
Maven assumed you wanted to add a sub-module to an existing Maven project. This failed because your existing POM file was "jar" project (e.g. normal Java code) when it was expecting a "pom" project.
This happens when you run the generate command from within the same folder where you ran the mvn archetype:create-from-project from (or in this case, the POM you generated made maven think this was an existing project).
Ernestas answer worked because they switched to a different folder and ran the same command.
Well, I tried the following commands and it worked for me:
> cd C:\Temp
> mvn archetype:generate -DgroupId=selenium.web.driver -DartifactId=OMS_selenium_Test -Dversion=1.0-SNAPSHOT -DinteractiveMode=false
EDIT
Now I have my project in: C:\Temp\OMS_selenium_Test

Out-of-tree build with maven. Is it possible?

I start learning packaging for several distros (currently Cygwin and Debian).
They have requirement to build system to allow out-of-tree build (synonym out-of-source build):
http://wiki.debian.org/UpstreamGuide#Out-of-Tree_Builds
To work-around "dumb" build system for example cygport recommend use lndir (from xutils project):
lndir ${S} ${B}
cd {B}
...build-commands...
I read mvn(1) man page but doesn't found anything appropriated. Next I just try:
$ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
...
$ pwd
/maven/simple
$ ls
my-app
$ mvn -f my-app/pom.xml compile
...
[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) # my-app ---
[INFO] Compiling 1 source file to /maven/simple/my-app/target/classes
As you can see target directory created in source root hierarchy while I look for a way to avoid this.
Is it possible out-of-tree build with maven? And how?
You could do like this to get it in your current working directory:
<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>com.stackoverflow</groupId>
<artifactId>Q13173063</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<buildDir>${user.dir}</buildDir>
</properties>
<build>
<directory>${buildDir}</directory>
</build>
</project>
Then you can issue
mvn -f my-app/pom.xml compile
And it will give you your classes in the current working directory.
And easily change to another output directory:
mvn -f my-app/pom.xml -DbuildDir=/tmp/build compile
It might be as simple as having a
<build>
<directory>/your/build/directory</directory>
</build>
in your pom.xml. /your/build/directory need not be in the source tree and can be parameterized using the usual ${...} syntax.
Cheers,

Categories