I have a project downloaded from git. Here is the link to source code https://github.com/dwdyer/reportng, I have downloaded it and now I am trying to create a JAR file and then want to attach it to maven repository. When I compile it using mvn compile and mvn package, it gives me the same INFO message, and in my target folder there is a jar file is created. But only pom.xml and pom.properties are shown inside it, instead of whole hierarchy of compiled class files replaced with Java files
Maven is very picky about following a very specific folder layout in the given project (which you can override but it is not really intended to do so).
Instead you may want to just install the generated jar file directly in your local repository using the mvn install:install command.
If you want to script this, see Multiple install:install-file in a single pom.xml for instructions on how to create a pom.xml doing this.
First of all, you have to debug problem:
cd reportng/
mvn -e clean install
-e switch on errors' trace.
If everything ok, install will add jar just created jar file to your local repository.
Then it will be available as dependency to any project:
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
</dependency>
By the way:
Jar is available in maven central, so referencing it as dependency, having compile time internet connection, will be enough.
This can be solved by changing the Maven strusture. Maven must contain src/main/java, whereas in the program it is src/java/main
Related
I am trying to execute a jar file, in terminal.
Jar created in intellij:
Build -> Build Artifacts -> Clean
Build -> Build Artifacts -> Build
Execution cmd:
java -jar helloworld.jar
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
at main.java.com.example.helloworld.sampleclass.mymethod(sampleclass.java:53)
at main.java.com.example.helloworld.sampleclass.main(sampleclass.java:14)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject
I have added the following dependency also.
Pom.xml Dependency:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
The code executes perfectly when I try to run in intellij, but I am getting this error when I run with the jar. Any suggestions would be great.
Ensure that you have correctly configured the artifact in (File -> Project Structure -> Artifacts) settings. Most likely, you have not specified the libraries that should be added to the class-path in the Output Layout tab.
I can assume that you created an artifact before adding a dependency. Try to remove the current artifact configuration and create a new one. IntelliJ IDEA will prepare a jar file with all necessary dependencies based on one of the following settings.
See the links below to learn how to manage Artifacts in IntelliJ IDEA:
Work with Artifacts
Artifacts
i know, it's not very intuitive, but maven doesn't actually embed your dependencies into your jar, unless you explicitly ask for it. If you open your jar like a zip (because jar files are essentially zip files with class files in them), you'll see that the class that you see an error about is actually not there.
Personally, rather than using the intellij export (I'm not sure if it just builds the jar the same way maven does), I would use maven directly with mvn package command (optionally mvn package -DskipTests if you don't want to execute your unit tests every time you build your jar). mvn package will build the jar and put it somewhere int the ./target directory.
And then, unless you want to specify the path to your jar dependencies in the command line every single time you run your app, you also will want to use the maven's shade plugin. The shade plugin unzips all the dependencies that you specify in the pom.xml and puts them into your new jar along with the code of your own application: http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
Sorry, I know it gets a little bit complicated here, but if you want to have an executable while using maven dependencies, shading is your best option.
In my case I had to extract the maven library to output root.
Project settings(Ctrl + Alt + Shift + S) -> Artificats -> Output Layout -> Move element to output root.
Many of maven's plugins run on the current project's pom.xml file.
Sometimes I need to run a certain plugin on an artifact I downloaded from the repository.
For instance I'm downloading sparkjava using dependency:get like that:
mvn dependency:get -Dartifact com.sparkjava:spark-core:2.5.4
I would like then download all sparkjava's dependencies sources using dependency:sources like that:
mvn dependency:sources -Dartifact com.sparkjava:spark-core:2.5.4
Or even better, run dependency:sources directly on the artifact:
mvn dependency:sources -DinputPom=locationToRepository/com/sparkjava/2.5.4/spark-core-2.5.4.pom
Is it possible to do?
The maven dependency plugin sources goal tells Maven to resolve all dependencies and their source attachments, and displays the version.
You can specifically includes or excludes artifacts by using optional parameters. See the maven dependency plugin documentation.
in my company, I have a Java project:
common: This contains classes that are used for several projects
GUI: This project contains a GUI for common
Without Maven, I add common to the class path, I can build the project. But how to setup the POM.xml and the Eclipse workspace when I want to build the GUI with Maven?
I also want to package the app into a JAR later, so I tried to setup the gui as .jar package
But then how to assign a Maven type to the common project?
Ideally, I could import "common" to the Maven project?
UPDATE:
Ok, it seems the mvn package command is able to resolve the "common" project when I add as local dependency see this under GUI. Still a bit confused about whether to use "pom", "maven-plugin" or "module" - anyone can add some links/clarifications, when to use what approach?
Declare common as usual maven dependency in GUI.
If it isn't maven project, add it to local repository as shown there How to add local jar files in maven project?
I would follow this steps
Create a local maven repository to store your custom jars. Which nothing but a proper directory structure with pom files in it.
Create your sub projects(common, gui) and package them as jar.
Install the local jars to local mvn repository.
Use the jars as dependency from your project.
Example:
Create a master project on ${master_project} location and your subprojects on ${master_project}/${common} and ${master_project}/${gui} location.
Create mvn repository in: ${master_project}/local-maven-repo
Specify your local repository location in your subprojects pom.xml located in ${master_project}/${gui}/pom.xml
<repository>
<id>local-maven-repo</id>
<url>file:///${project.parent.basedir}/local-maven-repo</url>
</repository>
Install your jars in local mvn repository
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where: <path-to-file> the path to the file to load
<group-id> the group that the file should be registered under
<artifact-id> the artifact name for the file
<version> the version of the file
<packaging> the packaging of the file e.g. jar
Finally use them as regular dependency
<dependency>
<groupId>org.company</groupId>
<artifactId>common</artifactId>
<version>0.9.0-SNAPSHOT</version>
</dependency>
Reference for this answer.
Here's further resources for Maven multiple module projects
install the maven plugin in your eclipse using the following link.
This will enable maven in your eclipse. Then right click on your project and click Configure and you will see Convert to Maven Project option. This will convert your existing project to a maven project. You can configure your project to be build as a .jar, .war etc. while converting from the wizard. The following is the tutorial on how to do it... link
I want to add jpoller.jar as a maven dependency, so I edited my pom.xml file to include it:
<dependency>
<groupId>org.sadun</groupId>
<artifactId>jpoller</artifactId>
<version>1.5.2</version>
</dependency>
Now, as expected, when I compile from the command line I get an error because jpoller isn't in any of the repositories I have listed in my pom, nor could I find one for it. Although I could create a repository for it, I'd rather not at this point. Thus, I get the following error:
[INFO] Failed to resolve artifact.
Missing:
---------- 1) org.sadun:jpoller:jar:1.5.2
Try downloading the file manually
from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=org.sadun -DartifactId=jpoller -Dversion=1.5.2 -Dpackaging=jar -Dfile=/path/to/file
How can I do this from the M2Eclipse plugin on machines where the maven CLI isn't available?
How can I do this from the M2Eclipse plugin on machines where the maven CLI isn't available?
Well, simply do it from Eclipse. First, get that jpoller jar and save it somewhere on your file system. Then, in Eclipse, from the top bar, Run > Run Configurations... then right-click on Maven Build and configure the New_configuration freshly created:
Select an arbitrary Base directory
Fill the Goals with install:install-file
Add parameters for each required parameters, without the -D prefix (e.g. file as Parameter name and /path/to/file as Value and so on for groupId, artifactId,packaging and version).
And run this configuration. Or... just install Maven.
The install command automates the creation of a folder structure in ~/.m2 and pom.xml file for the dependency artifact. This can be done manually. OR You can simply copy the ~/.m2/{group}/{artifact} folder from a machine that does have mvn installed.
Edit: This tool will help you find public repositories for a given dependency.
Edit2: See http://maven.apache.org/guides/mini/guide-coping-with-sun-jars.html for an explination of the process of installing dependencies manually. Note that most sun jars are now available in the java.net repository http://download.java.net/maven/2/
I have 2 Projects namely Project_1 and Project_2.
Both projects are Maven and I am using Netbeans.
I want to include the jar of Project_1 in Project_2 which I am doing like this.
The problem is when I include the jar I do not get any compile time error, however I get a NoClassDefFoundError exception at runtime.
When I include the Project_1 in Project_2 by performing the steps mentioned here. (The Open Project example). I do not get any errors. Neither runtime nor compile time.
Can you please explain me what am I missing here?
Update
Project_2 is deployed on a Server which is not in my local machine however Project_1 is in my local machine.
Inclusion of Project_1 into Project_2 as a project was done for testing in my local machine.
First of all, a good rule of thumb to adopt is never use the system scope and system path to pull in dependencies. In my experience there's always a better way :-)
If Project_2 depends on Project_1, then first install it's jar into the local repository:
cd Project_1
mvn clean install
Watch the output you'll discover the jar is placed somewhere under the following directory:
$HOME/.m2/repository
Once this is done the jar will be available as a normal dependency to the second build
cd Project_2
mvn clean compile
The local repository ensures the projects are now decoupled from each other. Assuming you're using snapshot revisions of Project_1, the Project_2 build will always retrieve the latest revision built and tested.
Update
You should use a Maven repository manager to share jars between machines/teams. Recommendations are contained in the following answer:
Share jar of the module with another team
How to configure Maven to use a repository like Nexus is described in it's documentation.
As described in the deploy plugin documentation you'll need to add a distributionManagement section to your POM (detailing the repository URL) and then upload the project's jar to your repository as follows:
cd Project_1
mvn clean deploy