Difference between revisions of "IntelliJ Setup"

From Kolmafia
Jump to navigation Jump to search
(Added Test Folder designation, because IJ doesn't always automatically detect it (thanks, PhilMasterPlus))
(Update guide, adding screenshots from IntelliJ IDEA 2021.2 Community edition)
Line 1: Line 1:
This guide assumes that you have a recent enough version of JAVA JDK, ANT, and IntelliJ Idea. Completing [[Compiling from Source]] is a requirement for this guide, although experienced developers can skip to whatever step is appropriate.
+
[https://www.jetbrains.com/idea/ IntelliJ IDEA] is an IDE for developing Java applications. This guide assumes that you have a recent enough version of Java JDK, Ant, and IntelliJ IDEA. Completing [[Compiling from Source]] is a requirement for this guide, although experienced developers can skip to whatever step is appropriate.
  
== Build and Config Project Setup ==
+
The following instructions are based on IntelliJ IDEA 2021.2 Community Edition. Instructions for other versions/editions of IntelliJ may be slightly different.
=== Load the project into IntelliJ ===
+
 
 +
== Create a Project ==
 +
=== Choice 1: Create a new project ===
 +
''If you already have a working copy of KoLmafia's source code, you can go to choice 2 instead.''
 +
 
 +
# Start IntelliJ IDEA. In the ''Welcome to IntelliJ IDEA'' dialog, click '''Get from VCS'''.<br>[[File:Intellij-setup-c2021.2-welcome.png|300px]]
 +
# In the ''Get from Version Control'' dialog, select the '''Repository URL''' tab.
 +
## In ''Version control'', select '''Subversion'''.<br>[[File:Intellij-setup-c2021.2-get-from-vcs.png|300px]]
 +
## Add a new repository location. To do so, click the '''+''' button next to ''Repositories''.<br>[[File:Intellij-setup-c2021.2-get-from-vcs-subversion.png|300px]]
 +
### In the ''New Repository Location'' popup, enter the URL to KoLmafia's Subversion repository. Currently, this is https://svn.code.sf.net/p/kolmafia/code.<br>[[File:Intellij-setup-c2021.2-new-repository-location.png|200px]]
 +
### Click '''OK''' to close the popup.
 +
## Once the repository has been added, '''click the repository URL to highlight it''', then click '''Check Out'''.<br>[[File:Intellij-setup-c2021.2-get-from-vcs-check-out.png|300px]]
 +
# If the ''Destination Directory'' popup appears, choose a directory to check out the working copy.<br>''Caution'': Don't choose a common directory like <samp>C:\Users\&lt;username&gt;\Documents</samp>. Instead, create a new directory under it (by clicking the folder icon) and give it an appropriate name (e.g. <samp>kolmafia</samp>).<br>[[File:Intellij-setup-c2021.2-choose-working-copy-dir.png|200px]]<br>Click '''OK''' to continue.
 +
# If the ''SVN Checkout Options'' popup appears, leave everything as-is and click '''OK'''.
 +
# If the ''Subversion Working Copy Format'' popup appears, choose the latest version (which is <samp>1.8 format</samp> as of writing) and click '''OK'''.
 +
# IntelliJ will generate a new project. If it asks you whether to add <code>kolmafia.iml</code> to Subversion, you may click '''Cancel''' to skip it.
 +
 
 +
=== Choice 2: Load an existing working copy into IntelliJ ===
 
# From the IntelliJ IDEA '''File''' Menu, choose '''New'''>'''New Project from Existing Sources...'''
 
# From the IntelliJ IDEA '''File''' Menu, choose '''New'''>'''New Project from Existing Sources...'''
 
#Choose the directory with the kolmafia build.xml file and press '''Open'''
 
#Choose the directory with the kolmafia build.xml file and press '''Open'''
Line 11: Line 28:
 
=== Add the Ant Build File ===
 
=== Add the Ant Build File ===
 
# Open the Ant Toolbar Item (typically on the right side, and not very large)
 
# Open the Ant Toolbar Item (typically on the right side, and not very large)
# Choose the '''+''' button to add a build file
+
# Click the '''+''' button to add a build file
 
# Choose build.xml from the file selector and press Open
 
# Choose build.xml from the file selector and press Open
 
# Verify that a list of ANT targets appears in the Ant Toolbar
 
# Verify that a list of ANT targets appears in the Ant Toolbar
  
 +
== Configure Project ==
 
=== Check Project Settings ===
 
=== Check Project Settings ===
# From the '''File''' Menu, choose '''Project Structure...'''
+
# In the top menu, select '''File''' > '''Project Structure...''' to launch the ''Project Structure'' dialog.
# Select '''Project''' on the Left Menu
+
# In the left menu, select '''Project Settings''' > '''Project'''
## set the '''Name''' to kolmafia
+
## set the '''Name''' to <kbd>kolmafia</kbd>
## set the '''Project SDK''' to any SDK version 1.8 or higher
+
## set the '''Project SDK''' to a version installed on your system. '''This must be at least 1.8'''.
## set the project language level to 8
+
## set the project language level to '''8'''
## set the project compiler output to the build subdirectory
+
## set the project compiler output path to the <kbd>&lt;project dir&gt;\build</kbd> subdirectory<br>[[File:Intellij-setup-c2021.2-project-structure-project.png|300px]]
# Select the '''libraries''' tab
+
# In the left menu, select '''Project Settings''' > '''Libraries'''
## Choose the '''+''' button to add library
+
## Add a project library with the name <kbd>kol-library-jars</kbd>:
## Select Type: Java
+
### Click the '''+''' button (''New Project Library''), then select '''Java''' to add a new project library<br>[[File:Intellij-setup-c2021.2-project-structure-libraries-new.png|300px]]
## Open the lib/jar directory and select all jars
+
### If the ''Select Library Files'' popup appears, navigate to <code>&lt;project dir&gt;\lib\jar</code> directory and select ''all'' JAR files. You can <kbd>shift</kbd>+click the first and last JAR files to select multiple files at once.<br>[[File:Intellij-setup-c2021.2-project-structure-libraries-select-jars.png|300px]]<br>Click '''OK''' to close the popup.
## Select add to kolmafia ''default is to name the library after the first jar. This can be changed on the next step.''
+
### If the ''Choose Modules'' popup appears, ensure that <samp>kolmafia</samp> is highlighted, then click '''OK'''.<br>[[File:Intellij-setup-c2021.2-project-structure-libraries-choose-modules.png|300px]]
## Select the '''Name''' field and enter "kol-library-jars"
+
### If you followed the steps above, IntelliJ will create a project library using the name of the first JAR file.<br>To rename it, select the library, then select the '''Name''' field and enter <kbd>kol-library-jars</kbd>.<div><ul><li style="display: inline-block;"> [[File:Intellij-setup-c2021.2-project-structure-libraries-rename-library.png|thumb|none|300px|Before renaming]]</li><li style="display: inline-block;"> [[File:Intellij-setup-c2021.2-project-structure-libraries-renamed.png|thumb|none|300px|After renaming]]</li></ul></div>
## Choose the '''+''' button to add another library
+
## Repeat the steps above to add a project library with the name <kbd>kol-source-jars</kbd>, selecting all JAR files under <code>&lt;project dir&gt;\src\jar\</code>.
## Open the src/jar directory and select all jars
+
## Repeat the steps above to add a project library with the name <kbd>kol-test-jars</kbd>, selecting all JAR files under <code>&lt;project dir&gt;\lib\testjar\</code>.
## Select add to kolmafia
+
## Click '''Apply'''.
## Select the '''Name''' field and enter "kol-source-jars"
+
# In the left menu, select '''Project Settings''' > '''Modules'''
# Select '''Modules''' in the Left Menu
+
## Select the '''Dependencies''' tab
## Choose the '''Sources''' tab
+
### If you followed the instructions so far, this section will have libraries with incorrect names (i.e. using the names of the first JAR file in each library). Select them all and click '''-''' to remove them.<br>[[File:Intellij-setup-c2021.2-project-structure-modules-remove-bad-libraries.png|300px]]
### Right-click the '''lib''' and '''src''' folders and verify that each is marked as ''Sources''
+
### Add the correct project libraries by clicking '''+''', then '''Library'''.<br>[[File:Intellij-setup-c2021.2-project-structure-modules-add-library.png|300px]]
### Right-click the '''test''' folder and verify that it is marked ''Test''
+
#### In the ''Choose Libraries'' dialog, select all project libraries we added so far, then click '''Add Selected'''.<br>[[File:Intellij-setup-c2021.2-project-structure-modules-choose-libraries.png|300px]]
## In the '''Paths''' tab, select '''Inherit project compile output path'''
+
## Select the '''Sources''' tab
# Close the Project Settings window
+
### Right-click the '''lib''' and '''src''' folders. If the  and verify that each is marked as ''Sources''
 +
### Right-click the '''test''' folder. If the folder is not marked as '''Tests''', click the menu item to check it.<br>[[File:Intellij-setup-c2021.2-project-structure-modules-sources.png|300px]]
 +
## Select the '''Paths''' tab
 +
### Select '''Inherit project compile output path'''<br>[[File:Intellij-setup-c2021.2-project-structure-modules-paths.png|300px]]
 +
# Click '''OK''' to close the ''Project Structure'' dialog
  
 
=== Add Run/Debug Configuration ===
 
=== Add Run/Debug Configuration ===
# From the '''Run''' Menu choose '''Edit Configurations...'''
+
# In the top menu, select '''Run''' > '''Edit Configurations...''' to launch the ''Run/Debug Configurations'' dialog.
# Choose the '''+''' button to add a new configuration
+
# Click the '''+''' button, then choose '''Application''' to add a new configuration
# Select '''Application'''
 
 
# In the '''Name''' Field, enter "KoLmafia"
 
# In the '''Name''' Field, enter "KoLmafia"
# In the '''Main Class''' Field, select the Selector '''[...]''', wait for it to populate the field, and choose KoLmafia
+
# Click the icon that looks like a document ("Browse...") to the right of the '''Main Class''' field.
 +
## If the ''Choose Main Class'' popup appears, wait until IntelliJ populates the list of possible main classes, then choose '''<samp>KoLmafia</samp>''' and click '''OK'''
 
# Add any VM or program arguments needed (typically none)
 
# Add any VM or program arguments needed (typically none)
# for the working directory, add the directory of your KoLmafia user data (e.g. ~/Library/Application\ Support/kolmafia )
+
# Enter the working directory. This is where KoLmafia will store your user data when launched from inside IntelliJ (e.g. <samp>C:\Users\&lt;user&gt;\Documents\kolmafia-workdir</samp>).<br>''Caution'': This should be different from the project directory!
# Add the following "before launch" tasks. For IntelliJ IDEA 2020.3, click ''Modify options'' at top right and select ''Add before launch task''. For older versions of IntelliJ, set the "before launch" tasks at bottom (may need to scroll to see the bottom of the configurations window).
+
# Add the "before launch" tasks:
## Select the '''+''' button at the bottom of the  the before launch group
+
## Click '''Modify options''' at the top right and select '''Add before launch task'''
## Select Run ANT target '''set.released.false''' and drag it above the Build task.
+
## Add the '''set.released.false''' Ant task:
## Select Run ANT target '''set.version''' and drag it above the Build task.
+
### Click the '''+''' button to the right of ''Before launch'', then '''Run Ant target'''.
## Select Run ANT target '''unset.properties'''.  Leave it below the Build Task
+
### If the ''Choose Ant Target to Execute'' popup appears, select '''set.released.false''' and click '''OK'''.
# Choose '''Apply'''
+
## Repeat the steps above to add the '''set.version''' Ant task
# Choose '''Close'''
+
## Repeat the steps above to add the '''unset.properties''' Ant task
 +
## Drag-and-drop the tasks to ensure that each task is executed in the following order:
 +
### Run Ant target 'set.released.false'
 +
### Run Ant target 'set.version'
 +
### Build
 +
### Run Ant target 'unset.properties'
 +
# Click '''OK''' to close the ''Run/Debug Configurations'' dialog.
  
 
Test by pressing the Run arrow next to your config name in the toolbar at the top of the window.
 
Test by pressing the Run arrow next to your config name in the toolbar at the top of the window.

Revision as of 09:43, 2 August 2021

IntelliJ IDEA is an IDE for developing Java applications. This guide assumes that you have a recent enough version of Java JDK, Ant, and IntelliJ IDEA. Completing Compiling from Source is a requirement for this guide, although experienced developers can skip to whatever step is appropriate.

The following instructions are based on IntelliJ IDEA 2021.2 Community Edition. Instructions for other versions/editions of IntelliJ may be slightly different.

Create a Project

Choice 1: Create a new project

If you already have a working copy of KoLmafia's source code, you can go to choice 2 instead.

  1. Start IntelliJ IDEA. In the Welcome to IntelliJ IDEA dialog, click Get from VCS.
    Intellij-setup-c2021.2-welcome.png
  2. In the Get from Version Control dialog, select the Repository URL tab.
    1. In Version control, select Subversion.
      Intellij-setup-c2021.2-get-from-vcs.png
    2. Add a new repository location. To do so, click the + button next to Repositories.
      Intellij-setup-c2021.2-get-from-vcs-subversion.png
      1. In the New Repository Location popup, enter the URL to KoLmafia's Subversion repository. Currently, this is https://svn.code.sf.net/p/kolmafia/code.
        Intellij-setup-c2021.2-new-repository-location.png
      2. Click OK to close the popup.
    3. Once the repository has been added, click the repository URL to highlight it, then click Check Out.
      Intellij-setup-c2021.2-get-from-vcs-check-out.png
  3. If the Destination Directory popup appears, choose a directory to check out the working copy.
    Caution: Don't choose a common directory like C:\Users\<username>\Documents. Instead, create a new directory under it (by clicking the folder icon) and give it an appropriate name (e.g. kolmafia).
    Intellij-setup-c2021.2-choose-working-copy-dir.png
    Click OK to continue.
  4. If the SVN Checkout Options popup appears, leave everything as-is and click OK.
  5. If the Subversion Working Copy Format popup appears, choose the latest version (which is 1.8 format as of writing) and click OK.
  6. IntelliJ will generate a new project. If it asks you whether to add kolmafia.iml to Subversion, you may click Cancel to skip it.

Choice 2: Load an existing working copy into IntelliJ

  1. From the IntelliJ IDEA File Menu, choose New>New Project from Existing Sources...
  2. Choose the directory with the kolmafia build.xml file and press Open
  3. Choose Create Project from Existing Sources and press Next
  4. Enter the name KoLmafia and press Next
  5. Verify that the project is visible to IDEA and press Finish

Add the Ant Build File

  1. Open the Ant Toolbar Item (typically on the right side, and not very large)
  2. Click the + button to add a build file
  3. Choose build.xml from the file selector and press Open
  4. Verify that a list of ANT targets appears in the Ant Toolbar

Configure Project

Check Project Settings

  1. In the top menu, select File > Project Structure... to launch the Project Structure dialog.
  2. In the left menu, select Project Settings > Project
    1. set the Name to kolmafia
    2. set the Project SDK to a version installed on your system. This must be at least 1.8.
    3. set the project language level to 8
    4. set the project compiler output path to the <project dir>\build subdirectory
      Intellij-setup-c2021.2-project-structure-project.png
  3. In the left menu, select Project Settings > Libraries
    1. Add a project library with the name kol-library-jars:
      1. Click the + button (New Project Library), then select Java to add a new project library
        Intellij-setup-c2021.2-project-structure-libraries-new.png
      2. If the Select Library Files popup appears, navigate to <project dir>\lib\jar directory and select all JAR files. You can shift+click the first and last JAR files to select multiple files at once.
        Intellij-setup-c2021.2-project-structure-libraries-select-jars.png
        Click OK to close the popup.
      3. If the Choose Modules popup appears, ensure that kolmafia is highlighted, then click OK.
        Intellij-setup-c2021.2-project-structure-libraries-choose-modules.png
      4. If you followed the steps above, IntelliJ will create a project library using the name of the first JAR file.
        To rename it, select the library, then select the Name field and enter kol-library-jars.
        • Before renaming
        • After renaming
    2. Repeat the steps above to add a project library with the name kol-source-jars, selecting all JAR files under <project dir>\src\jar\.
    3. Repeat the steps above to add a project library with the name kol-test-jars, selecting all JAR files under <project dir>\lib\testjar\.
    4. Click Apply.
  4. In the left menu, select Project Settings > Modules
    1. Select the Dependencies tab
      1. If you followed the instructions so far, this section will have libraries with incorrect names (i.e. using the names of the first JAR file in each library). Select them all and click - to remove them.
        Intellij-setup-c2021.2-project-structure-modules-remove-bad-libraries.png
      2. Add the correct project libraries by clicking +, then Library.
        Intellij-setup-c2021.2-project-structure-modules-add-library.png
        1. In the Choose Libraries dialog, select all project libraries we added so far, then click Add Selected.
          Intellij-setup-c2021.2-project-structure-modules-choose-libraries.png
    2. Select the Sources tab
      1. Right-click the lib and src folders. If the and verify that each is marked as Sources
      2. Right-click the test folder. If the folder is not marked as Tests, click the menu item to check it.
        Intellij-setup-c2021.2-project-structure-modules-sources.png
    3. Select the Paths tab
      1. Select Inherit project compile output path
        Intellij-setup-c2021.2-project-structure-modules-paths.png
  5. Click OK to close the Project Structure dialog

Add Run/Debug Configuration

  1. In the top menu, select Run > Edit Configurations... to launch the Run/Debug Configurations dialog.
  2. Click the + button, then choose Application to add a new configuration
  3. In the Name Field, enter "KoLmafia"
  4. Click the icon that looks like a document ("Browse...") to the right of the Main Class field.
    1. If the Choose Main Class popup appears, wait until IntelliJ populates the list of possible main classes, then choose KoLmafia and click OK
  5. Add any VM or program arguments needed (typically none)
  6. Enter the working directory. This is where KoLmafia will store your user data when launched from inside IntelliJ (e.g. C:\Users\<user>\Documents\kolmafia-workdir).
    Caution: This should be different from the project directory!
  7. Add the "before launch" tasks:
    1. Click Modify options at the top right and select Add before launch task
    2. Add the set.released.false Ant task:
      1. Click the + button to the right of Before launch, then Run Ant target.
      2. If the Choose Ant Target to Execute popup appears, select set.released.false and click OK.
    3. Repeat the steps above to add the set.version Ant task
    4. Repeat the steps above to add the unset.properties Ant task
    5. Drag-and-drop the tasks to ensure that each task is executed in the following order:
      1. Run Ant target 'set.released.false'
      2. Run Ant target 'set.version'
      3. Build
      4. Run Ant target 'unset.properties'
  8. Click OK to close the Run/Debug Configurations dialog.

Test by pressing the Run arrow next to your config name in the toolbar at the top of the window.

Optional Steps

  • Setup SVN to update from IntelliJ (can still be done from the command line or a tool)
    1. TODO: FIXME...
  • To use the Reformat Code feature, download the KoLMafia Style Definitions and import them in the IntelliJ Code Style Preferences.