This question already has answers here:
Maven: add a dependency to a jar by relative path
(10 answers)
Closed 6 years ago.
Currently I'm having the problem that I have an Java project which was created with eclipse and which should now be builded with maven. I haven't created this project I just need to migrate it. The problem here is that I have a bunch of 3rd party jar's which need to be included to properly build the project. I know nothing about these jars (and I actually don't care what they do and where the came from).
I managed to find resources on how to add these jar's via command line to ma local repro and I further created a pom.xml which does the job for me (so I don#t need to use the command line) but somehow its not working properly. I doen't matter if I use the command line or the pom.xmo, non of the two approaches works for me. When i run the 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>
<parent>
<groupId>common</groupId>
<artifactId>common.master</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>common</groupId>
<artifactId>org.proj4j</artifactId>
<name>org.proj4j</name>
<version>0.1.0</version>
<packaging>jar</packaging>
<build>
<sourceDirectory>src/</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>install1</id>
<phase>package</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>proj4j-0.1.0.jar</file>
<groupId>common</groupId>
<artifactId>org.proj4j</artifactId>
<name>proj4j</name>
<version>0.1.0</version>
<packaging>jar</packaging>
</configuration>
</execution>
<execution>
<id>install2</id>
<phase>package</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>proj4j-support-0.1.0.jar</file>
<groupId>common</groupId>
<artifactId>org.proj4j</artifactId>
<name>proj4j-support</name>
<version>0.1.0</version>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
the jars are put into my local repro under the specified group id, artifact ID etc. But the compiler still fails with an Unknown Symbol Error which says that the required class could not be found during compilation. I guess the reason are the artifact ID and the group ID because I came up with them. They are not related to my 3rd party jars at all. But I also don't know the proper group ID / artifact ID's because i know nothing about the jars. They are also partially self written by colleges.
What can I do to make maven find the required libs and to resolve all missing classes. Like I already said, with eclipse everything runs fine and without any problems. Shouldn't it be possible put the jar somehow into one folder in my local repro and tell the compiler If you search for anything start looking here ?
See if one of the below works for you:
How to add local jar files in maven project?
Maven: add a dependency to a jar by relative path let me know if it helps
They explain in details what needs to be done..
You can put your jar manually in your repo. You dont must know nothing about this jars, the artifact-id and the group-id affect the route in the repo and the name of the dependencie that compiler will look for.
I recommend you add some dependencie that exists in maven repo and observe the route and name of file that the pom creates in your local repo, after that sure you look the way that pom.xml works.
Related
A few days ago i started with Maven. I have to put only a few of my dependencies in my generated jar file. This is needed because my code is only a plugin (Minecraft Plugin) executed by an api (Minecraft Server Software Spigot). Now the Problem is, that my Plugin depends on an other api (json-simple-1.1).
The last days i tried to edit the maven shade plugin to get the wished result. I failed, and now i did it in this way:
maven include the json-simple-1.1 api, i needing for my plugin
eclipse include the spigot api (Minecraft server software), which will executing my plugin
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>de.falco.essentialsXXX</groupId>
<artifactId>EssentialsXXX-bungeecord</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Basic class for every Plugin
</description>
<build>
<sourceDirectory>src</sourceDirectory>
<!-- COMPILE -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- BUILD -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-json</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
When i now execute 'mvn clean install' (in the right directory) i get many many errors. That make completely sense. Maven can not find types or classes and everything else comeing from the spigot-api.
My Problem is, that this isnt a real error because when the spigot-api execute my plugin i have the classes and types i need. Maven dont know that and dont compile my Programm :(
At this point a have no idea what to do. I read so many articles but i couldnt find a solution. Every article say ohhh an error here try to use tags and the right api values. That isnt what i need.
I need something like a "bypass" attribute for the compiler so the compiler know "yes this is an error but the coder knows what he does"
If you need something for compilation, it needs to be a Maven dependency.
So take that artifact, install it in your local repository and add it as dependency.
Then your compilation process will probably work.
Note that using a dependency does not mean that you have to include the dependency into the resulting jar.
I'm trying to have multiple package hierarchies in multiple locations in a Java project. To explain further: our own software is in the packages com.company.V2.*. I now want to add these external repositories:
org.freedesktop.dbus
org.freedesktop.NetworkManager
but unlike in previous versions of Netbeans, it seems there is no way to specify multiple directories for these separate packages. Under Project Properties, Sources, there is one setting Source Folder, which is where the com.company.V2.* hierarchy is. There is no way to say 'for com.company look in /dir1/dir2; for org.freedesktop.dbus look in /dir4/dirZ etc.' Obviously Netbeans can do this, because classes in the java.lang.* hierarchy are found, for example.
There are two consequences:
Maven cannot find the source when I build.
Netbeans cannot find the source when I control-click and shows multiple errors in the editor windows relating to methods having wrong parameters etc.
I've checked-out the two external repositories into the project root. I fixed the Maven problem by adding this to the POM:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>dbus-java/dbus-java/src/main/java</source>
<source>kk-dbus-nm-java/src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
but this doesn't fix the editor window errors. It doesn't even fix the Maven problem properly because it is necessary to clean-and-rebuild (shift-F11) after each source change; otherwise I get a run-time error, which indicates to me that Maven isn't always finding stuff in the same place:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: org.freedesktop.dbus.connections.impl.DBusConnection.addSigHandler
at com.company.V2.net.Wifi.openBusConnection(Wifi.java:38)
at com.company.V2.net.Wifi.<clinit>(Wifi.java:23)
One way of fixing all this would be to use symlinks so that Maven and Netbeans see all packages in one hierarchy, but regrettably we have to support developers on Windoze / NTFS and in typical MS fashion, the simple matter of symlinking becomes ridiculously complicated under Windoze. I have created the symlinks and they solve all the problems given above, but I would prefer a 'proper' solution and not one involving a hacky workaround.
For the sake of anyone else looking, here is the best I have so far.
My project really only depends directly on org.freedesktop.NetworkManager, which in turn depends on org.freedesktop.dbus. So in my project pom.xml I have:
<repositories>
<repository>
<id>kk-dbus-nm-java</id>
<name>NetworkManager</name>
<url>file:///home/myself/projects/kk-dbus-nm-java</url>
</repository>
</repositories>
and
<dependency>
<groupId>kkdev.dbus</groupId>
<artifactId>org.freedesktop</artifactId>
<version>1.0-KKDev</version>
</dependency>
then /home/myself/projects/kk-dbus-nm-java/pom.xml is:
<?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>kkdev.dbus</groupId>
<artifactId>org.freedesktop</artifactId>
<version>1.0-KKDev</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.github.hypfvieh</groupId>
<artifactId>dbus-java</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
I have the following module structure:
powercontrol
powercontrol-core
pom.xml
powercontrol-data
pom.xml
powercontrol-gui
pom.xml
powercontrol-ui
pom.xml
pom.xml
Now I want that the GUI (Graphical User Interface) and UI (Command Line User Interface) can be executed by the client.
I tried to use the maven shade plugin inside the GUI and UI, but this makes it really a mess.
I prefer:
A jar file with all the third party dependencies (log4j etc).
A jar file (or maybe lib folder?) with all the project modules.
A'n executor for the GUI and UI.
Example:
powercontrol/
bin/
gui
ui
lib/
third-party.jar
powercontrol-core.jar
powercontrol-data.jar
powercontrol-gui.jar
powercontrol-ui.jar
I'm a bit stuck with getting a good structure now, where should I start?
All feedback, suggestions etc are welcome. Thank you in advance!
UPDATE 8/28/2015
I made a new module named: powercontrol-dist that will be executed as last in the Maven lifecycle. This module will generate a lib folder and copy all the dependencies from the powercontrol-gui and powercontrol-cli to this folder.
Now I have 2 questions!
Question 1
Is this a good way to go? Or is there a better way?
powercontrol-dist/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>
<parent>
<groupId>nl.nberlijn.powercontrol</groupId>
<artifactId>powercontrol</artifactId>
<version>1.0</version>
</parent>
<artifactId>powercontrol-dist</artifactId>
<packaging>pom</packaging>
<name>PowerControl Dist</name>
<description>Dist</description>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>powercontrol-gui</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>powercontrol-cli</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Output:
powercontrol/
bin/
gui.exe
ui.exe
lib/
third-party-lib.....jar
third-party-lib.....jar
third-party-lib.....jar
powercontrol-core.jar
powercontrol-data.jar
powercontrol-gui.jar
powercontrol-cli.jar
Question 2
Also I want to make two .exe files "gui.exe and cli.exe" referencing to the powercontrol-gui.jar and the powercontrol-cli.jar.
Is adding a mainclass to the manifest inside the pom.xml in the powercontrol-gui and powercontrol-cli module enough?
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
If you want two executable JARs (gui.jar, ui.jar), you should add the shade plugin to those two modules, so that as part of each module, a standalone executable JAR is built. Both of the JARs will contain all the third-party stuff as well. You cannot create a standalone executable JAR where parts of the dependencies are in an external JAR (unless you do you own classloader magic or unless you also have to specify the external JAR on the command line).
If you are stuck with the maven shade plugin, you should tell us what problem exactly you have. Typically these can be resolved. A common problem is that certain files need to be "merged" when a shaded JAR is created, in particular files in META-INF e.g. used by Spring or by the Java Service Locator mechanism. The shade plugin offers support for such merging, but it needs to be configured for the case at hand.
Btw. I'd recommend calling the command line version "cli.jar" - "ui" sounds like "gui".
Ok, since you updated your question and now seem to be asking for a "native launcher" (exe file) instead of an executable JAR file - those are completely different things.
Launching an exe file from the command line:
C:\> gui
Launching an executable JAR file from the command line
C:\ java -jar gui.jar
To get the first, you need to create a native launcher that internally invokes Java. A project that might support you in that task could be launch4j - they also seem to provide a Maven plugin.
I have existing two maven projects, I want to combine them after build into one project.
I have not created these projects module wise, the two projects are separate maven projects.
Below is the POM xml code part from existing projects.
Project One pom.xml
<groupId>com.olex</groupId>
<artifactId>olex-reg</artifactId>
<version>1.0</version>
<packaging>war</packaging>
Project Two pom.xml
<groupId>com.olex</groupId>
<artifactId>olex-qba</artifactId>
<version>1.0</version>
<packaging>war</packaging>
Combine Project pom.xml
<groupId>com.olex</groupId>
<artifactId>olex-war</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
I want to combine these projects into one like olex-war, A complete project.
After build I want all the code to be copied to this olex-war project.
Please suggest / provide hint if anyone aware of such scenario.
Thanks in advance.
You could start with a pom like this, that extract all the content of your old war files into content directory. Then you could package everything in that directory (except for web.xml and other metadata) in your new war.
<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.olex</groupId>
<artifactId>olex-war</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>olex-war Maven Webapp</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>olex-war</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>unpack</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.olex</groupId>
<artifactId>olex-qba</artifactId>
<version>${project.version}</version>
<packaging>war</packaging>
</artifactItem>
<artifactItem>
<groupId>com.olex</groupId>
<artifactId>olex-reg</artifactId>
<version>${project.version}</version>
<packaging>war</packaging>
</artifactItem>
</artifactItems>
<excludes>WEB-INF/web.xml</excludes>
<includes>**/*.class, **/*.jar, **/*.properties</includes>
<outputDirectory>${project.build.directory}/content</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
You should combine your projects manually. Many elements (like config files) need manual merging anyway and you want to avoid to accidentally loose something from one project because the other overwrites it quietly.
This would be a horrible mind-dulling task if there were not Version Control Systems to help you with the merging.
First set up a version control system (VCS). It will help in many other situations as well. I recommend git, but if you have another already up and running that should do it just fine, I won't use any fancy specials here.
Then create an empty folder for the new project and copy all the data from the first project into it. Do all replacements as you see fit (web.xml, pom.xml, other config files) and commit it to the VCS.
This will write this version of all files into the VCS and you cannot loose them again, so if you happen to make a mistake, no worries, just revert all changes back to this point.
Then copy the content of the second project over it overwriting everything thats duplicate.
Now you can use the "show differences" feature of your VCS to see what you'll need to merge. Most likely you can do so in a graphic tool so that makes it easy.
Most of them will be "unknown" that is, the new file does not exist in the VCS, yet. Most likely you can just add them to the VCS and be done with them.
More interesting are the ones that are "modified". That means the file already existed and the other project contained them as well. You will have to merge them into a combined version that is from now on valid for your new project.
When you are done you should have all the files (including pom.xml) necessary for the new project ready. Commit them to the VCS to not loose this state in the future.
From the forums that I followed I tried some ways to find a way to deploy mutliple wars using tomcat plugin in maven but I could not succeed.
I created a third project and used three projects in order to deploy them but I have not done it. Could you please tell me way to do it ?
Best Regards
Alper Kopuz
Here is the pom.xml that I used :
<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>
tr.com.provus.pays
</groupId>
<artifactId>PAYSGroupProject</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>../PAYSWeb</module>
<module>../PAYSDashboard</module>
<module>../PAYSStaticWeb</module>
</modules>
<name>PAYSGroupProject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<executions>
<execution>
<id>deploy</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
I guess you cannot use the tomcat plugin on a pom type project, try instead to configure the plugin into one of the war projects and include the others as webapp dependencies with something like that :
<configuration>
<webapps>
<webapp>
<contextPath>/PAYSWeb</contextPath>
<groupId>tr.com.provus.pays</groupId>
<artifactId>PAYSWeb</artifactId>
<version>${project.version}</version>
<type>war</type>
<asWebapp>true</asWebapp>
</webapp>
<webapp>...</webapp>
</webapps>
</configuration>
Look also at this post (but unanswered)
Each webapp will need a different context root which is supplied to the tomcat7 maven plugin with the "path" value.
You will deploy each web app from its own POM independently. But since you have a pom type project that causes the others to build, you should be able to redeploy all three at once.
Note that there are two ways to deploy using this plugin:
You can deploy without the war. It just compiles the java files and deploys them directly to tomcat.
You can deploy the war. Maven will have to build the war and then it gets deployed to Tomcat. This is more like a production deployment and helps you verify the war will deploy correctly.
So. Move your plugin XML to each of the three "modules" pom files. They will have type 'war'. Then add this under configuration:
<path>paysWeb</path>
under the <configuration> tag for the first 'module'. Of course, you use the different names for the <path> for each of the 'module's.
There is more info here: http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/usage.html