Cannot run java project after converting to Maven in Eclipse - java

After some hours of trying I give up. After converting my java project to maven in Eclipse, I can't seem to run the application via the main class.
Have changed source folder to src/main/java and added some changes to my pom file, but still dosen't work. What am I forgetting?
I'm totally new to Maven, but have read alot stack posts, but none seem to solve the issue.
pom file:
<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.NorthQStandAloneJava</groupId>
<artifactId>NorthQStandAloneJava</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<description>
Simple UI based program that can switch on a Qplug from NorthQ
</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.Controller.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Have already done the following:
1 - Run install maven
2 - Run as java application and pointing to the main class in the Controller folder
Hope someone can tell me what I'm doing wrong.

You must change:
<packaging>pom</packaging> to <packaging>jar</packaging>
and
<mainClass>com.Controller.Main</mainClass> to <mainClass>Controller.Main</mainClass>
Plus: remember java code conventions (packages must be lowercase)

Related

Maven (eclipse) use shaded jar instead of original jar

I have a project, where the packages will be relocated (shaded jar)
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>artifact-child</artifactId>
<name>artifact-child</name>
<parent>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>${revision}</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<!-- <shadedArtifactAttached>true</shadedArtifactAttached> -->
<relocations>
<reloaction>
<pattern>example</pattern>
<shadedPattern>${super-package}.example</shadedPattern>
</reloaction>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And I have a second project, which need the shaded jar at runtime.
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testversion</groupId>
<artifactId>testv</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testv</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>artifact-child</artifactId>
<version>0.0.1</version>
<classifier>shaded</classifier>
</dependency>
</dependencies>
</project>
My question:
Eclipse find the other project in his workspace and use directly the source code.
For example: I must write import example.myclass instead of import org.example.myclass.
In some cases, this can be a problem. Is it possible to say, what maven or eclipse should use the "shaded jar" instead of the original source code?
Must I create a online repository, so maven can only download the shaded jar?
I found two other stackoverflow posts (with no result):
Maven Eclipse multi-module shaded dependency
How to install shaded jar instead of the original jar
SOlVED:
My Mistakes:
1. version of the parent must declare directly not via properties
2. Forgot to run "Maven Install"
Solution:
Maven run without errors, but Eclipse use the open project and not the shaded jar.
Found the solution here: How can I download a single raw file from a private github repo using the command line? .
Open the properties of the Project. Under the Tap Maven, remove the check from "Resolve dependencies from Workspace projects"
If the shaded jar is your dependendy, you just reference the classes/packages as they are defined in the shaded jar.
If Eclipse shows an error, Eclipse is wrong (which is often the case). First of all, try to build your project with something like clean verify. Then you see whether you really have errors (or if Eclipse made them up).
If Eclipse errors bother you, try ALT+F5.

In Maven how to exclude src files in compilation when having custom src path?

I was trying to convert old java projects into maven build, but facing difficulties excluding source files:
when I set goal to install for the parent project to compile and build jar files for all the projects, it still tries to compile the mentioned excluded java file. Below is my pom.xml for that project:
<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.app</groupId>
<artifactId>CCAPS.Impl</artifactId>
<version>1.0.0</version>
<parent>
<groupId>com.app</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
<relativePath>../Maven.Convertion/parent</relativePath>
</parent>
<properties>
...
</properties>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/adjudication/mapper/BusinessAddendumItemDBOMapper.java</exclude>
<exclude>**/adjudication/mapper/ContactInfoDBOMapper.java</exclude>
<exclude>**/adjudication/mapper/IncomeDBOMapper.java</exclude>
<exclude>**/adjudication/mapper/IncomeItemDBOMapper.java</exclude>
<exclude>**/adjudication/mapper/InternationalAddressDBOMapper.java</exclude>
<exclude>**/adjudication/mapper/ReferralSourceDBOMapper.java</exclude>
<exclude>**/adjudication/interceptor/*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<sourceDirectory>${project.basedir}/src/net/ccapsws/ds/adjudication</sourceDirectory>
</build>
</project>
Last line for exclude is where the error throws when compiling:
[ERROR] \Maven Convertion\CCAPSAdjudication\DataSource.CCAPS.Impl\src\net\gc\ccapsws\ds\adjudication\interceptor\CCAPSDTOAuditInterceptor.java:[8,46] error: package net.ccapsws.validation.dictionary does not exist
The thing is I tried with the exclude declaration but since the src path is custom (${project.basedir}/src/net/ccapsws/ds/adjudicationm, for example, is the src path), I suspect maven doesn't recognize the path? Anyone can help with this?
NOTE that I'm not asking resource files, I want to compile files inside src folder but also willing to exclude specific java files during compilation.
EDIT: There's one another project referencing this project, but in the parent POM I'm putting that project after this project in the reactor sequence, so I don't think anywhere else is referencing this project.
The path issue looks similar to the problem answered by these links: https://stackoverflow.com/a/25262893/4055837 and https://stackoverflow.com/a/39450549/4055837

Maven : Using class from imported jar

I'm quite new in Maven.
I'm trying to do following:
Suppose we have to projects A and B. Project B needs to use some classfrom_A from jar imported from A
Here are definitions :
POM.xml of project A :
<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>A_Group</groupId>
<artifactId>A_Artifact</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>A_Project</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Here is POM.xml of project B (depends on A ) :
<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>B_Group</groupId>
<artifactId>B_Artifact</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>B_Project</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>A_Group</groupId>
<artifactId>A_Artifact</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Here is code snippet from project B:
import packageFromA.*;
public class App {
public static void main(String[] args) {
classFromA ca = new ClassFromA; //from packageFromA
ca.someFunction();
}
}
I run mvn install for A , then for B with no errors
But when trying to run code above from IntelliJ Idea, got error :
Error:(3, 18) java: package packageFromA does not exist
As I understand from maven docs : "compile dependencies are available in all classpaths, and they are packaged". But it seems that imported class still was not resolved . What was missing in the definitions ? Thanks in advance
UPDATE: When running from IntelliJ the issue was resolved after re-import. But when I run from command line
java projectB
Do I need explicitly set classpath of imported jars? If yes do I need point to i's location im local maven repository (.m2/repository) ? Thanks
In your POM for project B you can add a configuration for the maven-jar-plugin to generate a MANIFEST file which contains the classpath:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
...
</plugins>
</build>
</project>
This will set up the classpath attribute in your MANIFEST file, which expects all dependend jar libraries in a sub directory lib/ of your main jar. You need to copy these libraries manually in the lib directory.
To distribute your application with all dependend libraries using maven, you can use the maven assembly plugin.
Hope it helps!

Deploy maven project ear in websphere

I am using eclipse JUNO integrated with websphere 8.5.My problem is that,my project is in MAVEN module so each time i have to clean and install the MAVEN Project to generate the EAR.
After EAR generation, I have to remove the ibm related xml's inside the META-INF folder and WEB-INF folder and then I have to open the administrative console in websphere to deploy the EAR.This alone is taking huge amount of time.
Could any one please suggest a script to deploy and run the MAVEN project in websphere.
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.app</groupId>
<artifactId>SAMPLE_APP</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>SAMPLE APP</name>
<modules>
<module>SAMPLE_APP_Props</module>
<!-- Changes to UI Framework -->
<module>SAMPLE_APP_UIFile</module>
<module>SAMPLE_APP_Web</module>
<module>SAMPLE_APP_EAR</module>
<module>SAMPLE_APP_Forms</module>
<module>SAMPLE_APP_Filters</module>
<module>SAMPLE_APP_Logging</module>
<module>SAMPLE_APP_Security</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
The below 2Files i have to remove before i deploy the generated ear in websphere.
ibm-application-bnd.xmi
ibm-application-ext.xmi

Maven and Netbeans: how do I build project and get executable jar?

I've just started standard Maven -> Java Application in Netbeans 7.4.
It created "App" class (which has main method) by default. I also went to project's Properties->Run and set that class as a Main Class. I then built the project.
In the project's directory, I got the "target" folder with a couple of jars inside. None of them is the executable one. How do I simply fix the problem and get the executable jar at the end of each build?
thanks.
This may be a little lack of integration between Netbeans and Maven.
Doing "Properties->Run and set that Main Class" in the IDE doesn't make Maven to set the main class in the MANIFEST.MF of the generated jar: you have to explicitly say to Maven which is the main class by adding the <mainClass> tag to the configuration of the maven-jar-plugin.
For example if your pom.xml were:
<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.taringamberini</groupId>
<artifactId>AppTest</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.taringamberini.apptest.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
than in the target directory you would find the AppTest-1.0.0-SNAPSHOT.jar, and you should be able to run:
AppTest/target$java -jar AppTest-1.0.0-SNAPSHOT.jar
Hello World!

Categories