How to add a jar to the external library for a project w/o disappearing after sync and with imports working? - intellij-idea

Presently having an annoying time getting a jar added to the external library for the project, then in turn not disappear when syncing and the imports working. Adding I figured out and answered in another question. The issue is to keep persistent and accessible to a jar I create within the project for import.
I am not sure if I am missing a step or forgot to set something. I am using a modified gradle (forgegradle) to create the project. Have not found a solution with the documentation, or any for IntelliJ yet. I am generally new with IntelliJ, choosing to use it instead eclipse which I have previously used.
The goal is a to create an add-on for a mod to another jar. The main jar already in the external library from the start, now attempting to add the mod to it. I could modify the mod, but it is not my code, so rather not simply modify it directly and repackage it.

a jar added to the external library for the project, then in turn not disappear when syncing and the imports working
If you are using an external build tool / system that syncs with IntelliJ it is recommended to use that build tool / system to add dependencies.
IntelliJ will always take a backseat, and treat the build tooling / system as the source of truth for the project model as best it can.
You mention you are using gradle, I would recommend adding the dependency as a managed dependency that is, let gradle download it from a repository, and resolve the dependency/library itself.
If you can not do this, and you can't host a repository yourself, The next best recommendation I have, is creating a 'libs' folder inside the project, that contains jars that can't be found in repositories (They may be mods or plugins that were never published)
You can read up on how to add a library folder to gradle here: How to add local .jar file dependency to build.gradle file?

Related

Why is my library not being recognized after importing my project?

I'm using the JDBC library in my IntelliJ project (mysql-connector-java-5.1.40-bin.jar)
I put it into a folder named lib and used right click > Add as Library to add it as a library. It worked fine.
Later, I zipped the project and moved it to another computer. After unzipping it and importing it in IntelliJ, it gave me the error SQLException: No suitable driver found for jdbc:mysql, which means that it couldn't find the library.
I looked through the file explorer in IntelliJ and it showed both the jar and XML file, and the XML file was pointing to the right location (jar://$PROJECT_DIR$/lib/mysql-connector-java-5.1.40-bin.jar).
I added the jar file as a library again and now it works. But how can I make it work without having to readd it each time?
Use Gradle (or Maven) for building and dependency management. IDEA should import a Gradle (or Maven) project just fine.
The problem was that I was overwriting the IntelliJ Module Library (.iml) file.
When you import a project to the same folder, IntelliJ asks you whether you want to overwrite or reuse the IML file. Since Overwrite was the default option, I selected it, but that caused it to forget what libraries it was using.
By choosing Reuse instead, it works perfectly.

Play 2 dependency on a local module in Intellij Idea

I am kind of new to PlayFramework 2 and can not figure out how to resolve play 2 application dependencies. I need to add dependency on a local module loaded in IntellijIdea, not a jar file or repository.
While adding module dependencies in Idea project setting works just fine and ide itself is able to resolve them (autocompletion, imports etc are working), when trying to run in play2, its compiler cannot resolve any dependencies.
I manually configured Build.scala (adding val appDependencies = Seq("" % "" % "")) but am puzzled as to what resolvers I should use. I cannot point to a jar file, as it is a work in progress and such a file should be updated too often. Doing so would defeat the whole purpose of managed dependencies.
Play's main build mechanism uses SBT, which needs to know how to find all sources required for the build. There are several options for this:
make your module an SBT project itself and publish it to your local ivy repository. However that might be somewhat complex at this stage, and would involve adding your local ivy repository to the resolvers and re-publishing every time you change something in the module
declare your module as a sub-project. Play's documentation describes the process of working with sub-projects, I think this is the way you'd like to try out since then the idea command on Play's console will generate the IntelliJ configuration for the main application and the module.

IntelliJ IDEA and PlayFramework modules

After a lot of good comment about IDEA, I decided to give it a try. I downloaded the Community Edition and would like to use it for PlayFramework development.
I have followed official documentation and some other information gathered around, but I have not succeeded completely. When using a project with differents (play) modules, the different classes are not found.
For example when using Secure module, IDEA keeps complaining about not finding Secure.class. It has to be a classpath issue. I tried to attach Java source & classes ($PLAY/modules/secure/) in module settings (F4), class is still not found. Did I miss something?
BTW, I have done a play dependencies and play idealize, which seems to add another module Secure into project in IDEA.
Thanks,
The answer is to run the following:
play deps
play idealize
This forces the IDEA .iml project file to be refreshed with the updated class path entries for the new module (in this case Secure).
A issue I came across using IntelliJ and Playframework.
The log4j.properties file or log4j.xml file are not in classpath by default. You have to add the conf as a source folder in module settings.
You need to add the Secure module you have created in IDEA as a dependency to the main application module:
Go to File -> Project Structure
Choose the main module
Choose the Dependencies tab
Click Add -> Module Dependency
Choose the IDEA Module you created for the Play Secure module
Also make sure you have the correct source path selected for the Secure module in IDEA.

How to configure IntelliJ IDEA and/or Maven to automatically add directories with Java source code generated using jaxb2-maven-plugin?

In our project we are using jaxb2-maven-plugin to generate Java source code from XML Schema, which causes that our Maven module requires additional source code directory (in our case target/generated/main/java). Up to date I've been using Eclipse and maven-eclipse-plugin to import all the projects into Eclipse workspace. The plugin is (somehow) able to add the directory with generated source code automatically to Eclipse' .classpath file.
Recently I try to switch to (play with?) IntelliJ IDEA 9 (so I am a newbie in this environment) and I've noticed that additional source directory is not added during IDEA's importing process...
Is there any way I can configure IDEA/Maven to make importing directory with generated source code automatically?
The convention with Maven is to generate code in target/generated-sources/<tool>, for example target/generated-sources/jaxb2
Follow this convention and IDEA will add the folder as source folder (see IDEA-53198).
Generated code, using jaxb2-maven-plugin, was missing for me in Intellij 2017.1 whereas Eclipse Neon created it.
Fixed it from context menu of module by selecting 'Maven -> Generate Sources and Update Folders'.
Try with maven-jaxb2-plugin. If it does not work then it's IDEA problem.
In Maven you can add new source roots per configuration. Maven plugins can do this programmatically. This is for sure the case with maven-jaxb2-plugin. Then, if IDEA does not recognize it, then it's a problem on that side.
You can use the Maven Build Helper Plugin. It is located at http://www.mojohaus.org/build-helper-maven-plugin/
It allows to configure additional source roots. And the maven integration of IntelliJ will add the new source root. We are using this feature in quite a few builds and it works just fine. Tested with vers. 13 of IntelliJ IDEA.

How do I add my fragment to the list of required-plugins on an existing plugin

I currently have an existing plugin, which references a class from a required plugin. I have replaced this code with a reference to a class which is part of my fragment.
I am facing two issues.
If I import my fragment as a jar file, I am not able to see the changes I have made as the plugin running as an eclipse application results in a ClassNotFoundException
To overcome this, I link an additional source (of fragment) to the existing plugin project. However, the link uses an absolute path, and makes it unfit for deployment.
I want to be able to package the plugin with the code modification and be able to "depend" on my fragment code. Is there a way I can add my fragment as a dependency?
For example:
Plugin Project I am changing : org.eclipse.*.editor
it depends on org.eclipse.*.edit
I have a fragment mydomain.*.edit which has org.eclipse.*.edit as host plugin
I want org.eclipse.*.editor to pick up mydomain.*.edit
instead of org.eclipse.*.edit
ps: I have also tried packaging the jar file for the mydomain.*.edit in the plugins directory and try and pick it up from there, it doesnt show up on the list when I click add required plugins on the dependency tab on the plugin.xml file of the org.eclipse.*.editor
Please let me know if I am not clear enough, I will try and rephrase it.
Thanks in advance!
If I understand correctly what you want to do, I don't think that it's possible. You will have to try some other way.
Plugins have dependencies on other plugins. Fragments don't exist as separate runtime entities, but only as extensions of a plugin. So your plugin can only refer to the 'editor' plugin.
Classes provided by a fragment can't (and shouldn't) be accessed directly. They can be returned by the original plugin (A) if they are implementing an executable extension provided by plugin A.
If you refer to the fragment's code from another plugin (B), the classes will be loaded by plugin B's classloader and be different from the ones that are loaded by plugin A.
What is the purpose of your fragment? Do you want to get access to internal code in plugin A? Do you want to extend an eclipse editor?
If you want to extend functionality that the original plugin is not exposing as extensible, I think the only way is to write a plugin, extend the editor class from the original plugin, register it alongside the original one and use it instead.
[Edit] Maybe this link will explain better: Eclipse FAQ
Hope this helps,
Vlad
Thanks Vlad,
Your explanation was very helpful. Unlike the extension based architecture that is truly intended for fragments, I had to modify a certain component in the editor that was not exposed as part of the extension. This modification referred to an external project I created as an fragment but could have been a normal java project packaged a jar file that I could place in the classpath of the editor.
I was able to resolve the dependency issues by placing the jar file in class path, however when I export the plugins and related plugins as jar files and place it in the dropin directory, it does not install correctly. (Nor does placing the jar files in the plugins directory)
The eclipse editor that I am trying to modify uses the EMF project. I have kept the EMF project in the workspace inorder to resolve dependencies of the editor. However when I replace the EMF jar files bundled with eclipse with the one in the workspace, the files that I want to edit are not correctly recognized.
Is there another way of doing this?